android - 如何在 RecyclerView 中显示来自 Cloud FireStore 的对象数组
android - how to display the array of object from Cloud FireStore in the RecyclerView
我正在尝试将存储在 Cloud Firestore 中的数据显示到 RecyclerView,但是当我 运行 应用程序时,它只显示 itemList
以外的所有数据 map
正在显示
我没有收到任何错误,所以它导致我遇到瓶颈
我的适配器代码
Context context;
List<ViewOrderModel> viewOrderModelList;
public ViewOrderAdapter(Context context, List<ViewOrderModel> viewOrderModelList) {
this.context = context;
this.viewOrderModelList = viewOrderModelList;
}
@Override
public void onBindViewHolder(@NonNull ViewOrderAdapter.ViewHolder holder, int position) {
holder.userName.setText(viewOrderModelList.get(position).getUserName());
holder.date.setText(viewOrderModelList.get(position).getCurrentTime());
holder.totalPrice.setText(String.valueOf(viewOrderModelList.get(position).getTotalPrice()));
holder.address.setText(viewOrderModelList.get(position).getAddress());
holder.phone.setText(viewOrderModelList.get(position).getPhoneNumber());
ViewOrderModel viewOrderModel = viewOrderModelList.get(position);
holder.productName.setText(viewOrderModel.getProductName());
holder.quantity.setText(String.valueOf(viewOrderModel.getTotalQuantity()));
}
型号Class
public class ViewOrderModel implements Serializable {
String userName,phoneNumber,currentTime,address,documentID,productName;
long totalPrice;
List<ViewOrderModel> itemList;
int totalQuantity;
主要Activity
order_rec = findViewById(R.id.admin_view_order_rec);
order_rec.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
viewOrderModelList = new ArrayList<>();
viewOrderAdapter = new ViewOrderAdapter(this,viewOrderModelList);
order_rec.setAdapter(viewOrderAdapter);
firestore.collection("UserOrder")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (!queryDocumentSnapshots.isEmpty()){
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot docSnap : list) {
ViewOrderModel orderModel = docSnap.toObject(ViewOrderModel.class);
viewOrderModelList.add(orderModel);
}
viewOrderAdapter.notifyDataSetChanged();
}
}
});
有没有办法显示数据或任何其他与我在代码中所做的不同的方式
您需要两个不同的 class。例如,第一个是 ViewOrderModel
class,它将包含 address
、currentTime
、documentID
等字段。
第二个叫 Product
:
class Product {
String documentID, productName;
int productPrice, totalPrice, totalQuantity;
}
其中将保存与 itemList
中存在的对象对应的属性。所以你的 ViewOrderModel
class 应该是这样的:
public class ViewOrderModel implements Serializable {
String userName,phoneNumber,currentTime,address,documentID,productName;
List<Product> itemList;
}
我正在尝试将存储在 Cloud Firestore 中的数据显示到 RecyclerView,但是当我 运行 应用程序时,它只显示 itemList
以外的所有数据 map
正在显示
我没有收到任何错误,所以它导致我遇到瓶颈
我的适配器代码
Context context;
List<ViewOrderModel> viewOrderModelList;
public ViewOrderAdapter(Context context, List<ViewOrderModel> viewOrderModelList) {
this.context = context;
this.viewOrderModelList = viewOrderModelList;
}
@Override
public void onBindViewHolder(@NonNull ViewOrderAdapter.ViewHolder holder, int position) {
holder.userName.setText(viewOrderModelList.get(position).getUserName());
holder.date.setText(viewOrderModelList.get(position).getCurrentTime());
holder.totalPrice.setText(String.valueOf(viewOrderModelList.get(position).getTotalPrice()));
holder.address.setText(viewOrderModelList.get(position).getAddress());
holder.phone.setText(viewOrderModelList.get(position).getPhoneNumber());
ViewOrderModel viewOrderModel = viewOrderModelList.get(position);
holder.productName.setText(viewOrderModel.getProductName());
holder.quantity.setText(String.valueOf(viewOrderModel.getTotalQuantity()));
}
型号Class
public class ViewOrderModel implements Serializable {
String userName,phoneNumber,currentTime,address,documentID,productName;
long totalPrice;
List<ViewOrderModel> itemList;
int totalQuantity;
主要Activity
order_rec = findViewById(R.id.admin_view_order_rec);
order_rec.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
viewOrderModelList = new ArrayList<>();
viewOrderAdapter = new ViewOrderAdapter(this,viewOrderModelList);
order_rec.setAdapter(viewOrderAdapter);
firestore.collection("UserOrder")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (!queryDocumentSnapshots.isEmpty()){
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for (DocumentSnapshot docSnap : list) {
ViewOrderModel orderModel = docSnap.toObject(ViewOrderModel.class);
viewOrderModelList.add(orderModel);
}
viewOrderAdapter.notifyDataSetChanged();
}
}
});
有没有办法显示数据或任何其他与我在代码中所做的不同的方式
您需要两个不同的 class。例如,第一个是 ViewOrderModel
class,它将包含 address
、currentTime
、documentID
等字段。
第二个叫 Product
:
class Product {
String documentID, productName;
int productPrice, totalPrice, totalQuantity;
}
其中将保存与 itemList
中存在的对象对应的属性。所以你的 ViewOrderModel
class 应该是这样的:
public class ViewOrderModel implements Serializable {
String userName,phoneNumber,currentTime,address,documentID,productName;
List<Product> itemList;
}