所以這是我的配接器的源代碼:
package com.example.consigness;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.List;
public class ConsignmentAdapter extends ArrayAdapter<Consignment> {
TextView title_cList;
TextView consignor_cList;
TextView recipient_cList;
TextView desc_cList;
TextView status_cList;
//get Database
FirebaseFirestore db = FirebaseFirestore.getInstance();
User con;
User rec;
public ConsignmentAdapter(Context context, List<Consignment> object){
super(context,0, object);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.consignment_list,parent,false);
}
title_cList = convertView.findViewById(R.id.cTitle_cList);
consignor_cList = convertView.findViewById(R.id.cCons_cList);
recipient_cList = convertView.findViewById(R.id.cRec_cList);
desc_cList = convertView.findViewById(R.id.cDesc_cList);
status_cList = convertView.findViewById(R.id.cStatus_cList);
Consignment consignment = getItem(position);
title_cList.setText(consignment.getTitle());
consignor_cList.setText("By: " consignment.getConsignee());
readData(new FirestoreCallBack() {
@SuppressLint("SetTextI18n")
@Override
public void onCallback(User user) {
Log.d("CONSIGNOR READ OUTSIDE", con.getFname());
consignor_cList.setText("By: " con.getFname() ": " con.getPost() " at " con.getCname());
}
}, consignment.getConsignee());
recipient_cList.setText("To: " consignment.getRecipient());
readRec(new FirestoreCallBack() {
@SuppressLint("SetTextI18n")
@Override
public void onCallback(User user) {
Log.d("RECIPIENT READ OUTSIDE", rec.getFname());
recipient_cList.setText("To: " rec.getFname() ": " rec.getPost() " at " rec.getCname());
}
}, consignment.getRecipient());
desc_cList.setText(consignment.getDesc());
status_cList.setText("Status: " consignment.getStatus());
return convertView;
}
private interface FirestoreCallBack{
void onCallback(User user);
}
private void readData(ConsignmentAdapter.FirestoreCallBack firestoreCallBack, String id){
db.collection("Users")
.whereEqualTo("uid", id)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("CHECK", document.getId() " => " document.getData());
User user = document.toObject(User.class);
con = user;
}
firestoreCallBack.onCallback(con);
} else {
Log.d("CHECK", "Error getting documents: ", task.getException());
}
}
});
}
private void readRec(ConsignmentAdapter.FirestoreCallBack firestoreCallBack, String id){
db.collection("Users")
.whereEqualTo("uid", id)
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("CHECK", document.getId() " => " document.getData());
User user = document.toObject(User.class);
rec = user;
}
firestoreCallBack.onCallback(rec);
} else {
Log.d("CHECK", "Error getting documents: ", task.getException());
}
}
});
}
}
我想要做的是,從 firestore 查詢中更改我的兩個 Textview、consignor_cList 和 recipient_cList 的值。
最初,這兩個文本視圖都將文本值作為我的 Firestore 檔案的字串 uid。我想使用這些 id,查詢整個檔案并將檔案資料用于文本視圖。
前:
consignor_cList.setText("By: " consignment.getConsignee());
后:
readData(new FirestoreCallBack() {
@SuppressLint("SetTextI18n")
@Override
public void onCallback(User user) {
Log.d("CONSIGNOR READ OUTSIDE", con.getFname());
consignor_cList.setText("By: " con.getFname() ": " con.getPost() " at " con.getCname());
}
}, consignment.getConsignee());
我為此使用了 Firestore 回呼函式。
問題:不是代碼不起作用。這是。我可以從我的日志中看到我的程式有成功的查詢。然后這些查詢應該由 Textview 設定文本。但是但是,它只發生在第一個串列項而不是其余的,有時是第一個和最后一個,而不是其余的。
這必須是我的高級專案。我希望今晚有天才出現,給我一個解決方案。
我還將分享輸出的螢屏截圖。

uj5u.com熱心網友回復:
您不應將單獨的行視圖存盤為配接器中的類成員,也不應將 User 存盤在那里,因為每次創建行時它們都會被覆寫。而不是這個
TextView title_cList;
//... later in getView()
title_cList = convertView.findViewById(R.id.cTitle_cList);
在內部使用它getView()并將視圖作為類成員洗掉。
TextView title_cList = convertView.findViewById(R.id.cTitle_cList);
每一行都會呼叫其中的代碼getView(),但您的回呼稍后會在類成員更改為參考不同的行后運行。
您可能還應該從配接器中洗掉它們(盡管這可能不會導致當前錯誤,但如果您稍后嘗試將它們用于任何事情,它可能會引入其他錯誤):
User con;
User rec;
因為這些可以為每一行更改,而是在回呼中使用區域變數,可能是這樣的:
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d("CHECK", document.getId() " => " document.getData());
User user = document.toObject(User.class);
firestoreCallBack.onCallback(user);
}
uj5u.com熱心網友回復:
View finalConvertView1 = convertView;
readRec(new FirestoreCallBack() {
@SuppressLint("SetTextI18n")
@Override
public void onCallback(User user) {
Log.d("RECIPIENT READ OUTSIDE", rec.getFname());
recipient_cList = finalConvertView1.findViewById(R.id.cRec_cList);
recipient_cList.setText("To: " rec.getFname() ": " rec.getPost() " at " rec.getCname());
}
}, consignment.getRecipient());
我試圖以findViewById這種方式將模塊初始化()降低到轉換視圖中并且它起作用了
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496537.html
