dimanche 3 mars 2019

Searching a ListView that has been populated with data from a Firebase Realtime Database

I'm trying to add a search feature one of the activities in my android application. I currently have a ListView that displays all groups stored in the database. Each group is assigned a number which is what the search criteria will be. I tried using a query as well as an if statement on the client side and neither of them rendered any results.

Assigning values and calling the method:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_groups);

        databaseGroups = FirebaseDatabase.getInstance().getReference("groups");
        groups = new ArrayList<>();
        editTextGroupNo = findViewById(R.id.editTextSearchNo)
listViewGroups = findViewById(R.id.listViewGroups);


 buttonSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                groups.clear();
                String vaccinationInfo = editTextGroupNo.getText().toString();
                firebaseVaccinationSearch(vaccinationInfo);
            }
        });

Attempt 1: The query

private void firebaseVaccinationSearch(final String vaccinationInfo) {
        Query vaccinationSearchQuery = databaseGroups.orderByChild("groupNumber").startAt(vaccinationInfo).endAt(vaccinationInfo + "\uf8ff");
        vaccinationSearchQuery.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
                    Group group = groupSnapshot.getValue(Group.class);
                    groups.add(group);
                }
                GroupList groupList = new GroupList(ActivityAllGroups.this, groups);
                listViewGroups.setAdapter(groupList);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }

Attempt 2: The If Statement

private void firebaseVaccinationSearch(final String vaccinationInfo) {

    databaseGroups.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Group group = postSnapshot.getValue(Group.class);
                if (vaccinationInfo == group.getGroupNo()){
                    groups.add(group);
                }
                else {
                    Toast.makeText(ActivityAllGroups.this,"No Results", Toast.LENGTH_SHORT).show();
                }

GroupList groupList = new GroupList(ActivityAllGroups.this, groups);
                    listViewGroups.setAdapter(groupList);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
   });

}

Finally, here is the structure of the database:Database structure

Thanks for the help!

Aucun commentaire:

Enregistrer un commentaire