从 ListView 适配器中删除重复项

Remove duplicates from ListView adapter

我已经创建了许多数组适配器来从 Firebase 过滤 ListView。每当我在 UI 上 select 一个过滤器 ("In-Progress Tasks") 时,它就会工作,但是当我再次单击同一个过滤器 ("In-Progress Tasks") 时,它会创建该列表的副本。我正在努力寻找具有相似上下文的 SO 的答案。

如有任何帮助,我们将不胜感激。

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();
                }

在添加到列表之前确保没有重复的值:

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();
            }