我有一个相对简单的recyclerview设置。在我的片段中,我查询Firestore以获取用户输入的一些预选值。这些值之一实际上是同一Firebase实例中引用餐厅名称的另一个集合中的集合ID。我不想显示ID,而是要显示ID所属餐厅的名称。
现在在viewholder方法中,我查询其他集合以获取餐厅名称。
这是我最初查询的一部分:
mealplanDetailsArrayList = new ArrayList<>();
mealplanReference = firebaseFirestore.collection("Customer_Data").document(userId).collection("Meal_Plans");
mealplanReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
MealplanDetails mealplanDetails = document.toObject(MealplanDetails.class);
mealplanDetailsArrayList.add(mealplanDetails);
mealPlanDetailsAdapter = new MealPlanDetailsAdapter(mealplanContext, mealplanDetailsArrayList);
mealplanRecycler.setAdapter(mealPlanDetailsAdapter);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "on failure error:"+e);
}
});
这是我的适配器:
public class MealPlanDetailsAdapter extends RecyclerView.Adapter<MealPlanDetailsAdapter.MealPlanDetailsViewHolder> {
private Context adapterContext;
private ArrayList<MealplanDetails> mealplanDetailsArrayList;
private String restaurantName;
private final static String TAG = MealPlanDetailsAdapter.class.getSimpleName();
public MealPlanDetailsAdapter(Context adapterContext, ArrayList<MealplanDetails> mealplanDetailsArrayList) {
this.mealplanDetailsArrayList = mealplanDetailsArrayList;
this.adapterContext = adapterContext;
}
@NonNull
@Override
public MealPlanDetailsAdapter.MealPlanDetailsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(adapterContext);
View view = layoutInflater.inflate(R.layout.mealplan_card, viewGroup, false);
return new MealPlanDetailsViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MealPlanDetailsAdapter.MealPlanDetailsViewHolder mpDetailsViewHolder, int position) {
// Set restaurant name
mpDetailsViewHolder.tvRestaurant.setText(getRestaurantName(mealplanDetailsArrayList.get(position).getRestaurantId()));
Log.d(TAG, "restaurantId:" + mealplanDetailsArrayList.get(position).getRestaurantId() + " " + position);
}
private String getRestaurantName(final String restaurantId) {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
CollectionReference restaurantReference = firebaseFirestore.collection("Restaurant_Data");
restaurantReference.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
if (documentSnapshot.getId().equals(restaurantId)) {
restaurantName = documentSnapshot.getString("restaurantName");
}
}
}
});
return restaurantName;
}
@Override
public int getItemCount() {
return mealplanDetailsArrayList.size();
}
class MealPlanDetailsViewHolder extends RecyclerView.ViewHolder {
public TextView tvRestaurant;
public MealPlanDetailsViewHolder(@NonNull View itemView) {
super(itemView);
tvRestaurant = itemView.findViewById(R.id.tv_restaurant);
}
}
}
为了简单起见,为了简便起见,我删除了我正在加载到Viewholder中的其他一些数据,但没有任何问题,但是发生的是,在某些位置,我从查询中获得的餐厅名称(我正在适配器中进行操作)类)没有显示。该textview显示为空白。
有人知道为什么会发生这种情况以及如何解决吗?