Show only People with a specific value (Firebase)

2

I have a recyclerview that I charge from a database that I have in Firebase , I do it like this:

    databaseReference.child("Personas").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            listPersonas.add(dataSnapshot.getValue(Personas.class));
            displayPersonas(listPersonas);
        }

In Firebase it would be something like that,

People / id /

  • dataperson: value
  • show: value

Well, what I would like to achieve is that when the value of show is for example true that item is not shown in recyclerview the others that do not have that value in show yes.

How could I do this? Thanks!

    
asked by UserNameYo 08.06.2017 в 23:32
source

1 answer

2

Summarizing the comments:

To the list Persona s that manages the recyclerview you can filter it according to the attribute mostrar , creating a new list which would happen to your method displayPersonas

That way your listaPersonas stays the same and in the view you present a filtered list.

That you can achieve by simply iterating and adding the elements whose mostrar == true

databaseReference.child("Personas").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            listPersonas.add(dataSnapshot.getValue(Personas.class));
            displayPersonas(filtrarPersonas(listPersonas));
        }


private List<Persona> filtrarPersonas(List<Persona> original){
    List<Persona> personasAMostrar = new ArrayList<Persona>():
    if(original!=null){
        for(Persona p : original){
            if(p.getMostrar()){
                personasAMostrar.add(p); // o add(p.clone()), podria interesarte tener la misma referencia o no, eso depende de la logica de negocio de tu app
            }
        }
    }
    return personasAMostrar;
}

EDIT: If you are not interested in keeping listaPersonas with people with mostrar = false you can avoid filtering the list and simply not adding those elements:

databaseReference.child("Personas").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Persona p = dataSnapshot.getValue(Personas.class);
            if(p.getMostrar()){
               listPersonas.add(p);
              }        
            displayPersonas(listPersonas);
        }
    
answered by 08.06.2017 / 23:46
source