我当前正在开发需要更新阵列上特定值的应用程序。
CollectionReference collectionReference = mDb.collection("Booking");
mListenerRegistration = collectionReference.whereEqualTo("DriverUID", FirebaseAuth.getInstance().getCurrentUser().getUid())
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e != null) {
Toast.makeText(Maps.this, "Error During Loading", Toast.LENGTH_LONG).show();
} else {
ArrayList<Map<String, Object>> arrList = new ArrayList<>(); // looping through the array
String Did = "";
for (QueryDocumentSnapshot snapshots : queryDocumentSnapshots) {
Did = snapshots.getId();
String BookedID = snapshots.getData().get("bookingID").toString(); // get the booked key fro just listing the
arrList = (ArrayList<Map<String, Object>>) snapshots.getData().get("trips");
}
ArrayList<Map<String, Object>> finalArrList = arrList;
for (int i =0; i<finalArrList.size(); i++){
Log.d("ID IS ", "onEvent: "+finalArrList.get(i));
if(arrList.get(i).get("status").equals("NOT COMPLETED")){
Desription = finalArrList.get(i).get("Des").toString();
FromPlace = finalArrList.get(i).get("FromPlace").toString();
Price = finalArrList.get(i).get("Prices").toString();
Status = finalArrList.get(i).get("Status").toString();
ToPlace = finalArrList.get(i).get("ToPlace").toString();
UplaodTime =finalArrList.get(i).get("UploadedTime").toString();
AdminS = finalArrList.get(i).get("adminStatus").toString();
date3 =finalArrList.get(i).get("date").toString();
Fromaddres = finalArrList.get(i).get("fromAddress").toString();
FromTime = finalArrList.get(i).get("fromTime").toString();
Note1 =finalArrList.get(i).get("note").toString();
toAddress = finalArrList.get(i).get("toAddress").toString();
ToTime = finalArrList.get(i).get("toTime").toString();
gloabal = i;
break;
}
}
UpdateS.put("Des", Desription);
UpdateS.put("FromPlace",FromPlace);
UpdateS.put("Prices",Price);
UpdateS.put("Status", Status);
UpdateS.put("ToPlace", ToPlace);
UpdateS.put("UploadedTime", UplaodTime);
UpdateS.put("adminStatus", AdminS);
UpdateS.put("date", date3);
UpdateS.put("fromAddress", Fromaddres);
UpdateS.put("fromTime", FromTime);
UpdateS.put("note", Note1);
UpdateS.put("status", "COMPLETED");
UpdateS.put("toAddress", toAddress);
UpdateS.put("toTime", ToTime);
finalArrList.set(gloabal, UpdateS);
collectionReference.document(Did).update("trips",finalArrList);
Log.d(TAG, "onEvent id : " + Did);
}
}
});
在上面的代码中,到目前为止,我已经从“ Booking”集合中的数组“ trips”中检索了数据。退出过程后,我尝试使用检索到的数据更新特定位置,但是当状态为“未完成”时,我比较了它更新了整个阵列而不是所需位置。
顺便说一句,我试图从recyclerview视图传递项目位置,但是由于我从不同的数组中检索了数据,所以它不起作用。