1. Download our Official Android App: Forums for Android!

Firebase & RecyclerView

Discussion in 'Android Development' started by Ratchoss, Oct 24, 2017.

  1. Ratchoss

    Ratchoss Newbie
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    17
    Joined:
    Oct 24, 2017

    Oct 24, 2017
    17
    0
    25
    Hello, i'm new there.

    I have a trouble with my app, i made a RECYCLERVIEW with my firebase data as items, but i would like to filter it.

    I want to hide any data that has "connected: off" from the recyclerview list.
    upload_2017-10-24_10-4-35.png

    however, I want to display all the data that has "connected: on"
    upload_2017-10-24_10-6-49.png

    Here is my code that will help you in this process, I want to filter the "on" and "off", hide the "off" and display the "on".

    Code (Java):
    1.  
    2.         FirebaseRecyclerAdapter<Films,FilmsViewHolder> adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(
    3.                 Films.class,
    4.                 R.layout.salonview,
    5.                 FilmsViewHolder.class,
    6.                 //referencing the node where we want the database to store the data from our Object
    7.                 mDatabaseReference.child("Salons").getRef()
    8.         ) {
    9.             @Override
    10.             protected void populateViewHolder(FilmsViewHolder viewHolder, final Films model, int position) {
    11. ....
    12.  
    Thank you for helping me.
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,540
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,540
    12,713
    1,988
    Male
    Software developer
    South West of England
    Hi there. The code you've shown is not enough. Ideally we want to see what's in the "...." section. i.e. what's the structure of your items (Films?) that you're adding to the list.
    I'm guessing you need some logic in the populateViewHolder() method, to determine the value of the 'connected' property.
     
  3. Ratchoss

    Ratchoss Newbie
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    17
    Joined:
    Oct 24, 2017

    Oct 24, 2017
    17
    0
    25
    Oh sorry!

    That's the code for the "Salons Class" :
    Code (Java):
    1.         if (recycle != null) {
    2.             //to enable optimization of recyclerview
    3.             recycle.setHasFixedSize(true);
    4.         }
    5.         //using staggered grid pattern in recyclerview
    6.         mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    7.         recycle.setLayoutManager(mLayoutManager);
    8.  
    9.         //Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter
    10.         FirebaseRecyclerAdapter<Films,FilmsViewHolder> adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(
    11.                 Films.class,
    12.                 R.layout.salonview,
    13.                 FilmsViewHolder.class,
    14.                 //referencing the node where we want the database to store the data from our Object
    15.                 mDatabaseReference.child("Salons").getRef()
    16.         ) {
    17.             @Override
    18.             protected void populateViewHolder(FilmsViewHolder viewHolder, final Films model, int position) {
    19.                 viewHolder.tvFilmsName.setText(model.getEntreprise());
    20.                 viewHolder.tvFilsAdresse.setText(model.getAdresse());
    21.                 Picasso.with(Salons.this).load(model.getURL()).into(viewHolder.ivFilmsPoster, new Callback() {
    22.                     @Override
    23.                     public void onSuccess() {
    24.                         pd.dismiss();
    25.                     }
    26.  
    27.                     @Override
    28.                     public void onError() {
    29.  
    30.                     }
    31.                 });
    32.                 viewHolder.ivFilmsPoster.setOnClickListener(new View.OnClickListener() {
    33.                     @Override
    34.                     public void onClick(View v) {
    35.                         entreprise.clear();
    36.                         image.clear();
    37.                         entreprise.add(model.getEntreprise());
    38.                         image.add(model.getURL());
    39.  
    40.                         startActivity(new Intent(Salons.this, ProfileSalons.class));
    41.                     }
    42.                 });
    43.  
    44.             }
    45.         };
    46.  
    47.         recycle.setAdapter(adapter);
    48.  
    The "Films class" :

    Code (Java):
    1. public class Films {
    2.     public String entreprise;
    3.     public String gerant;
    4.     public String adresse;
    5.     public String fixe;
    6.     public String portable;
    7.     public String mail;
    8.     public String mdp;
    9.     public String photo;
    10.     public String connected;
    11.  
    12.     public Films(){
    13.  
    14.     }
    15.     public Films(String entreprise, String gerant, String adresse,String fixe, String portable, String mail, String mdp, String photo, String connected){
    16.        this.entreprise = entreprise;
    17.         this.gerant = gerant;
    18.         this.adresse = adresse;
    19.         this.fixe = fixe;
    20.         this.portable = portable;
    21.         this.mail = mail;
    22.         this.mdp = mdp;
    23.         this.photo = photo;
    24.         this.connected = connected;
    25.     }
    26.  
    27.     public String getEntreprise() {
    28.         return entreprise;
    29.     }
    30.  
    31.     public void setEntreprise(String entreprise) {
    32.         this.entreprise = entreprise;
    33.     }
    34.  
    35.  
    36.     public String getGerant() {
    37.         return gerant;
    38.     }
    39.  
    40.     public void setGerant(String gerant) {
    41.         this.gerant = gerant;
    42.     }
    43.  
    44.     public String getAdresse() {
    45.         return adresse;
    46.     }
    47.  
    48.     public void setAdresse(String adresse) {
    49.         this.adresse = adresse;
    50.     }
    51.  
    52.     public String getFixe() {
    53.         return fixe;
    54.     }
    55.  
    56.     public void setFixe(String fixe) {
    57.         this.fixe = fixe;
    58.     }
    59.  
    60.     public String getPortable() {
    61.         return portable;
    62.     }
    63.  
    64.     public void setPortable(String portable) {
    65.         this.portable = portable;
    66.     }
    67.  
    68.     public String getMail() {
    69.         return mail;
    70.     }
    71.  
    72.     public void setMail(String mail) {
    73.         this.mail = mail;
    74.     }
    75.  
    76.     public String getMdp() {
    77.         return mdp;
    78.     }
    79.  
    80.     public void setMdp(String mdp) {
    81.         this.mdp = mdp;
    82.     }
    83.  
    84.     public String getURL() {
    85.         return photo;
    86.     }
    87.  
    88.     public void setURL(String photo) {
    89.         this.photo = photo;
    90.     }
    91.  
    92.  
    93.     public String getConnected() {
    94.         return connected;
    95.     }
    96.  
    97.     public void setConnected(String connected) {
    98.         this.connected = connected;
    99.     }
    100.  
    101.  
    102. }
    Sorry for neglecting some points about this problem. You have the necessary classes I think? Thank you in advance.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,540
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,540
    12,713
    1,988
    Male
    Software developer
    South West of England
    Thanks, that's a lot better.
    So this is the interesting piece of code, but something I'm not really familiar with

    Code (Text):
    1.  
    2.                 Picasso.with(Salons.this).load(model.getURL()).into(viewHolder.ivFilmsPoster, new Callback() {
    3.                     @Override
    4.                     public void onSuccess() {
    5.                         pd.dismiss();
    6.                     }
    7.  
    8.                     @Override
    9.                     public void onError() {
    10.  
    11.                     }
    12.                 });
    13.  
    I don't know what 'Picasso' is, but it appears to be populating your viewHolder with data from the database?
    So my question is, what is 'ivFilmsPoster'? Is it a list?
    And I would imagine that your onSuccess() method is called when the data is loaded successfully. My thought is that you can iterate through the loaded data, and remove the items which have a connected value of "false".
     
  5. Ratchoss

    Ratchoss Newbie
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    17
    Joined:
    Oct 24, 2017

    Oct 24, 2017
    17
    0
    25
    Oh, Picasso allows you for hassle-free image loading in your application, it's better use.

    ivFilmsPoster is the "ImageView" which "model.getURL()" is his URL LINK. So it's not a list.

    This bellow is at the end of my "Salons class" :
    Code (Java):
    1.     public static class FilmsViewHolder extends RecyclerView.ViewHolder{
    2.  
    3.         TextView tvFilmsName, tvFilsAdresse;
    4.         ImageView ivFilmsPoster;
    5.  
    6.         public FilmsViewHolder(View v) {
    7.             super(v);
    8.             tvFilmsName = (TextView) v.findViewById(R.id.tv_name);
    9.             tvFilsAdresse = (TextView) v.findViewById(R.id.tv_adresse);
    10.             ivFilmsPoster = (ImageView) v.findViewById(R.id.iv_movie_poster);
    11.         }
    12.     }
    Oh, good idea, but how can i do this? (Checking if loaded data which have a connected value of "false" and hide it) please.

    Thanks a lot for your help.
     
  6. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #10
    Points:
    1,988
    Posts:
    8,540
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    8,540
    12,713
    1,988
    Male
    Software developer
    South West of England
    There is still not enough code shown. Please show the entire code for your list adapter class.
     
  7. Ratchoss

    Ratchoss Newbie
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    17
    Joined:
    Oct 24, 2017

    Oct 24, 2017
    17
    0
    25
    Okay, my bad!

    There is it:
    Salons.java
    Code (Java):
    1. public class Salons extends AppCompatActivity {
    2.  
    3.     //created for firebaseui android tutorial by Vamsi Tallapudi
    4.  
    5.     private StaggeredGridLayoutManager mLayoutManager;
    6.  
    7.     //Getting reference to Firebase Database
    8.     FirebaseDatabase database = FirebaseDatabase.getInstance();
    9.     DatabaseReference mDatabaseReference = database.getReference();
    10.  
    11.     RecyclerView recycle;
    12.  
    13.     ProgressDialog pd;
    14.  
    15.     int pos;
    16.  
    17.     List<String> ItemsIntoList;
    18.  
    19.     public static List<String> coiffures = new ArrayList<String>();
    20.  
    21.     DatabaseReference myRef;
    22.  
    23.     public static List<String> naming = new ArrayList<String>();
    24.  
    25.     public static List<String> entreprise = new ArrayList<String>();
    26.     public static List<String> image = new ArrayList<String>();
    27.  
    28.     public static List<String> listing = new ArrayList<String>();
    29.     public static List<String> balayage = new ArrayList<String>();
    30.     public static List<String> coloration = new ArrayList<String>();
    31.  
    32.     @Override
    33.     protected void onCreate(Bundle savedInstanceState) {
    34.         super.onCreate(savedInstanceState);
    35.         setContentView(R.layout.activity_salons);
    36.  
    37.         recycle = (RecyclerView) findViewById(R.id.my_recycler_view);
    38.  
    39.         naming.clear();
    40.  
    41.         pd = new ProgressDialog(this);
    42.         pd.setMessage("Chargement...");
    43.         pd.show();
    44.         pd.setCancelable(false);
    45.         pd.setCanceledOnTouchOutside(false);
    46.  
    47.         if (recycle != null) {
    48.             //to enable optimization of recyclerview
    49.             recycle.setHasFixedSize(true);
    50.         }
    51.         //using staggered grid pattern in recyclerview
    52.         mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
    53.         recycle.setLayoutManager(mLayoutManager);
    54.  
    55.         //Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter
    56.         FirebaseRecyclerAdapter<Films,FilmsViewHolder> adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(
    57.                 Films.class,
    58.                 R.layout.salonview,
    59.                 FilmsViewHolder.class,
    60.                 //referencing the node where we want the database to store the data from our Object
    61.                 mDatabaseReference.child("Salons").getRef()
    62.         ) {
    63.             @Override
    64.             protected void populateViewHolder(FilmsViewHolder viewHolder, final Films model, int position) {
    65.                 viewHolder.tvFilmsName.setText(model.getEntreprise());
    66.                 viewHolder.tvFilsAdresse.setText(model.getAdresse());
    67.                 Picasso.with(Salons.this).load(model.getURL()).into(viewHolder.ivFilmsPoster, new Callback() {
    68.                     @Override
    69.                     public void onSuccess() {
    70.                         pd.dismiss();
    71.                     }
    72.  
    73.                     @Override
    74.                     public void onError() {
    75.  
    76.                     }
    77.                 });
    78.                 viewHolder.ivFilmsPoster.setOnClickListener(new View.OnClickListener() {
    79.                     @Override
    80.                     public void onClick(View v) {
    81.                         entreprise.clear();
    82.                         image.clear();
    83.                         entreprise.add(model.getEntreprise());
    84.                         image.add(model.getURL());
    85.  
    86.                         startActivity(new Intent(Salons.this, ProfileSalons.class));
    87.                     }
    88.                 });
    89.  
    90.             }
    91.         };
    92.  
    93.         recycle.setAdapter(adapter);
    94.  
    95.     }
    96.     //ViewHolder for our Firebase UI
    97.     public static class FilmsViewHolder extends RecyclerView.ViewHolder{
    98.  
    99.         TextView tvFilmsName, tvFilsAdresse;
    100.         ImageView ivFilmsPoster;
    101.  
    102.         public FilmsViewHolder(View v) {
    103.             super(v);
    104.             tvFilmsName = (TextView) v.findViewById(R.id.tv_name);
    105.             tvFilsAdresse = (TextView) v.findViewById(R.id.tv_adresse);
    106.             ivFilmsPoster = (ImageView) v.findViewById(R.id.iv_movie_poster);
    107.         }
    108.     }
     
  8. Ratchoss

    Ratchoss Newbie
    Thread Starter
    Rank:
    None
    Points:
    25
    Posts:
    17
    Joined:
    Oct 24, 2017

    Oct 24, 2017
    17
    0
    25
    It's simple : I would like to show only data which have "connected : true" as child. and all other data which have "connected: false" will be hided.

    TIP : I know how to remove an item from the RecyclerView = viewHolder.itemView.setVisibility(View.GONE);

    Now my problem is i want to check if X has "connected:true" as child.

    Thanks.
     

Share This Page

Loading...