3

I have created a number of array adapters to filter a ListView from Firebase. Whenever I select a filter ("In-Progress Tasks") on the UI it works, however when I click on the same filter again ("In-Progress Tasks") it creates a duplicate of this list. I'm struggling to find an answer on SO with similar context.

Any help would be appreciated.

task_list.java

case R.id.inProgressStatus:

final ArrayList<String> filteredbyInProgress = new ArrayList<>();

final Query queryInProgress = FirebaseDatabase.getInstance().getReference("stores").child("Store 01").child("Task List")
                    .orderByChild("Status")
                    .equalTo("In-Progress");

final ArrayAdapter<String> arrayAdapter4 = new ArrayAdapter<String>(this, R.layout.tasks_layout, R.id.textTask, listTask);
            this.dbTasks.setAdapter(arrayAdapter4);

final ArrayAdapter<String> filterAdapter4 = new ArrayAdapter<String>(this, R.layout.tasks_layout, R.id.textTask, queryTask);
            this.dbTasks.setAdapter(filterAdapter4);


queryInProgress.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        //adding the key to an Arraylist to be referenced when deleting records
                        filteredbyInProgress.add(ds.getKey());
                        String name = ds.child("Name").getValue(String.class);

                        queryTask.add(name);
                    }

                    task_list.this.dbTasks.setAdapter(filterAdapter4);
                    arrayAdapter4.clear();
                    arrayAdapter4.notifyDataSetChanged();
                }

1 Answer 1

2

just make sure there are no duplicate value before adding into the list :

queryInProgress.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                queryTask.clear();
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    //adding the key to an Arraylist to be referenced when deleting records
                    filteredbyInProgress.add(ds.getKey());
                    String name = ds.child("Name").getValue(String.class);

                    if (!queryTask.contains(value)) {
                      queryTask.add(value);
                    }
                }

                task_list.this.dbTasks.setAdapter(filterAdapter4);
                filterAdapter4.notifyDataSetChanged();
                arrayAdapter4.clear();
                arrayAdapter4.notifyDataSetChanged();
            }
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry @faruk this gave me the exact same result as before
why there is two adapter? and why you notifydatasetchanged() listTask adapter not queryTask adapter?? @AlexBingham
this may sound a little confusing but one adapter contains all un-queried data (arrayAdapter) and the second adapter (filterAdapter) is the newly queried data. The ArrayAdapter is also used in each to access the data in an edit form. I hope that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.