我正在尝试从Firebase上获取数据(任务),当单击按钮时会遇到查询。我可以获取所有数据(任务),但是当我单击按钮以获取过滤的数据时,我没有收到预期的结果。
我已将非过滤适配器设置为当我单击按钮时将其清除,并且将filteredAdapter设置为无结果。
我接近正确了吗?
数据库结构
我的Firebase查询
final Query statusQuery = FirebaseDatabase.getInstance().getReference().child("Store 01").child("Task List")
.orderByChild("Status")
.equalTo("Complete");
用于获取过滤数据的代码
final ArrayAdapter<String> filterAdapter = new ArrayAdapter<String>(this, R.layout.tasks_layout, R.id.textTask, listTask);
this.dbTasks.setAdapter(filterAdapter);
statusButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
statusQuery.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
filteredTasks.add(ds.getKey());
String name = ds.child("Name").getValue(String.class);
String date = ds.child("Date").getValue(String.class);
String assigned = ds.child("Assigned to").getValue(String.class);
String type = ds.child("Type").getValue(String.class);
queryTask.add(name + "\n" + date + "\n" + assigned + "\n" + type);
Log.d("TAG", name);
}//for
task_list.this.dbTasks.setAdapter(filterAdapter);
arrayAdapter.clear();
arrayAdapter.notifyDataSetChanged();
}//onDataChange
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}//OnCancelled
});
}//OnClick
});
用于提取未过滤数据的代码
ValueEventListener eventListener = new ValueEventListener() {
//Searching the database and adding each task to list
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
//adding the key to an Arraylist to be referenced when deleting records
tasks.add(ds.getKey());
String name = ds.child("Name").getValue(String.class);
String date = ds.child("Date").getValue(String.class);
String assigned = ds.child("Assigned to").getValue(String.class);
String type = ds.child("Type").getValue(String.class);
listTask.add(name + "\n" + date + "\n" + assigned + "\n" + type);
Log.d("TAG", name);
}//for
task_list.this.dbTasks.setAdapter(arrayAdapter);
}//onDataChange
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}//OnCancelled
};
database.addListenerForSingleValueEvent(eventListener);
答案 0 :(得分:0)
final ArrayAdapter<String> filterAdapter = new ArrayAdapter<String>(this, R.layout.tasks_layout, R.id.textTask, listTask);
this.dbTasks.setAdapter(filterAdapter);
新的阵列适配器指向错误的arrayList“ listTask”,我将其指向“ queryTask”,这解决了该问题。
filterAdapter = new ArrayAdapter<String>(this, R.layout.tasks_layout, R.id.textTask, queryTask);