Query search android studio, firebase data listing problems

Asked

Viewed 275 times

0

Could someone help me? The app recovers data from child("grupos") in debugging, but when I tie the knot, for example, child("grupos").child("nome"), the application of crash nor in debugging to see if you’re recovering. What I’m doing wrong?

Database https://ibb.co/C1Gy8gs

DEBUG https://ibb.co/6b8Whw3

Error using child("groupos").child("nome");

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
    .activity.SalasActivity$1.search(SalasActivity.java:86)
    .activity.SalasActivity$1.access$000(SalasActivity.java:56)
    .activity.SalasActivity$1$1.onQueryTextChange(SalasActivity.java:78)






public class SalasActivity extends AppCompatActivity {

DatabaseReference ref;
ArrayList<Deal> list;
RecyclerView recyclerView;
SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_salas);

        ref = FirebaseDatabase.getInstance().getReference().child("grupos").child("nome");
        recyclerView = findViewById(R.id.rv);
        searchView = findViewById(R.id.searchView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if(ref != null){
            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (dataSnapshot.exists()){
                        list = new ArrayList<>();

                        for (DataSnapshot ds : dataSnapshot.getChildren())
                        {
                        list.add(ds.getValue(Deal.class));
                        }
                        AdapterClass adapterClass = new AdapterClass(list);
                        recyclerView.setAdapter(adapterClass);
                    }
                    if (searchView != null){
                        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                            @Override
                            public boolean onQueryTextSubmit(String query) {
                                return false;
                            }

                            @Override
                            public boolean onQueryTextChange(String s) {
                                search(s);
                                return true;
                            }
                        });
                    }
                }
                private  void  search(String str){
                    ArrayList<Deal> myList = new ArrayList<>();
                    for (Deal object : list){
                        if (object.getNome().toLowerCase().contains(str.toLowerCase())){
                            myList.add(object);

                        }
                    }
                    AdapterClass adapterClass = new AdapterClass(myList);
                    recyclerView.setAdapter(adapterClass);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(SalasActivity.this,databaseError.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }
}

Deal.class error E/Androidruntime: FATAL EXCEPTION: main Process: xxx.chat, PID: 11966 com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type .activity.Deal

    public class Deal {
    private String nome;
    private String id;
    private String membros;

    public Deal() {
    }

    public Deal(String nome, String id, String membros) {
        this.nome = nome;
        this.id = id;
        this.membros = membros;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMembros() {
        return membros;
    }

    public void setMembros(String membros) {
        this.membros = membros;
    }
}

1 answer

0

Hello, Marc!

To recover the nome, you will need the key group name. I would look more or less like this, for example, to get the group name

mDatabaseRef.child("grupos")
            .child("-L_KE54XVwBMans6Bh6") // Key do grupo
            .addValueEventListener(new ValueEventListener() {
                 @Override
                 public void onDataChange(DataSnapshot dataSnapshot) {
                     if (dataSnapshot.exists()) {
                       String nome = dataSnapshot.getValue().child("nome");
                     }
                 }
                 @Override
                 public void onCancelled(DatabaseError databaseError) {

                 }
             });

Whenever a group is added to the tree, it is added with a key. When you need to retrieve information from him, you need to know his key.

EDIT 1: You need to add the recovered data to your ArrayList;

EDIT 2: What happens is that you try to recover a field that is not of the type that is defined in your class Deal, probably the attribute membro. In the database, he appears to be a being array of objects or a type of list (ArrayList or List). I believe that for your purpose, your database is not structured correctly.

Suggestion

As your project is still in its infancy, I suggest you modify this structure and separate the membership list from the child groups. When creating the membership list, use the key of child groups to which it belongs, for example, to the first group

inserir a descrição da imagem aqui

Note that the key of child is the same as the group to which he belongs, as suggested. See also, the key of each member, can be their own identification within the database. So when you want to retrieve the membership list, do it this way

mDatabaseRef.child("membros")
                .child("-L_KE54XVwBMans6Bh6") // Key do grupo
                .addValueEventListener(new ValueEventListener() {
                     @Override
                     public void onDataChange(DataSnapshot dataSnapshot) {
                         if (dataSnapshot.exists()) {
                            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                mMembros.add(snapshot.getValue(Membro.class);
                            }
                         }
                     }
                     @Override
                     public void onCancelled(DatabaseError databaseError) {

                     }
                 });

There are some advantages to this: it makes it easier to get the membership list, ensures that the same member is not added twice or more in the same group and you won’t need to iterate through the groups to get the members.

This restructuring is the best solution to your problem as it is more scalable. Consider before creating the other parts of your project, read the documentation from Firebase Database of as structure the database (is a quick read, but will clarify some important points).

I hope this solves your problem!

  • thanks it worked out recovered, but my arraylist remains zeroed follows a debug image https://ibb.co/6m4dBqs

  • am having this error now, com.google.firebase.database.Databaseexception: Can’t Convert Object of type java.lang.String to type .activity.Deal I will update the question with the Deal class if you can help me would be grateful

  • @Marc edited my answer with the answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.