[英]Recyclerview Adapter: java.util.ArrayList cannot be cast to com.google.firebase.firestore.QuerySnapshot
I'm fairly new and I'm not familiar with using recyclerview adapter.我相当新,我不熟悉使用 recyclerview 适配器。 I have this method to to load the data into the recyclerview adapter.
我有这种方法可以将数据加载到 recyclerview 适配器中。 The data will be filtered using merged queries.
将使用合并查询过滤数据。 However the app crashed and I got java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.google.firebase.firestore.QuerySnapshot.
However the app crashed and I got java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.google.firebase.firestore.QuerySnapshot. Does anyone know how to fix this?
有谁知道如何解决这一问题?
private void loadData(){
if(modelArrayList.size()>0)
modelArrayList.clear();
storageReference = FirebaseStorage.getInstance().getReference();
Query query = collectionReference.whereArrayContains("pet", pettype);
Query query2 = collectionReference.whereArrayContains("service", "Pet Sitting");
Query query3 = collectionReference.whereArrayContains("petnum", petnumber);
Query query4 = collectionReference.whereArrayContains("region", reg);
Query query5 = collectionReference.whereArrayContains("subregion", subreg);
Task task = query.get();
Task task2 = query2.get();
Task task3 = query3.get();
Task task4 = query4.get();
Task task5 = query5.get();
Task combinedtask = Tasks.whenAllSuccess(task, task2, task3, task4, task5);
combinedtask.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot querySnapshot: task.getResult()){
model model = new model(querySnapshot.getString("firstname"),
querySnapshot.getString("lastname"),
querySnapshot.getString("description"),
querySnapshot.getString("userID"));
modelArrayList.add(model);
}
adapter = new My_recyclerview_adapter(Pet_sitting_result.this, modelArrayList);
recycler_view.setAdapter(adapter);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Pet_sitting_result.this, "Error", Toast.LENGTH_SHORT);
}
});
}
Since your combinedTask
is a result of several other Tasks, it is going to yield an List
objects as a result, not a QuerySnapshot
.由于您的
combinedTask
是其他几个任务的结果,因此它将产生一个List
对象,而不是QuerySnapshot
。 So, your success callback should declare that it accepts a List<Task<?>>
instead of a Task<QuerySnapshot>
.因此,您的成功回调应该声明它接受
List<Task<?>>
而不是Task<QuerySnapshot>
。 There will be one item the list for each task you passed to whenAllSuccess()
.您传递给
whenAllSuccess()
的每个任务的列表中都会有一项。 You will have to write code to pull the individual snapshots for each query out of the list and deal with them each separately as needed.您必须编写代码将每个查询的单个快照从列表中拉出,并根据需要分别处理它们。
You might want to familiarize yourself more with the Tasks API that you're using here and also study the API documentation for Tasks.whenAll() .您可能想进一步熟悉您在此处使用的任务 API并研究有关Tasks.whenAll()的 API 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.