我创建了许多阵列适配器来从Firebase过滤ListView。每当我在UI上选择一个过滤器(“正在进行的任务”)时,它就会起作用,但是,当我再次单击同一过滤器(“正在进行的任务”)时,它将创建此列表的副本。我正在努力寻找具有类似上下文的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();
}
答案 0 :(得分:1)
只需确保在添加到列表之前没有重复的值即可:
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();
}