gpt4 book ai didi

java - Recycler View 不显示从 Firestore 数据库获取的数据

转载 作者:行者123 更新时间:2023-12-01 16:24:16 25 4
gpt4 key购买 nike

我正在从 Firestore 数据库中获取一些数据,并将其显示在 RecyclerView 中。数据正在被提取到应用程序中,正如我通过 Log.d() 语句看到的那样。但是,数据不会出现在 RecyclerView 中。

我的MainActivity.java:

public class MainActivity extends AppCompatActivity {

FirebaseFirestore db;
List<MenuItem> foodList;
RecyclerView menulist;
MenuAdapter madap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference foodRef= db.collection("Food");

foodList = new ArrayList<>();
madap = new MenuAdapter(foodList);
menulist = (RecyclerView)findViewById(R.id.listview);
menulist.setHasFixedSize(true);
menulist.setLayoutManager(new LinearLayoutManager(this));
menulist.setAdapter(madap);

db.collection("Food").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot doc : task.getResult())
{
MenuItem mi = doc.toObject(MenuItem.class);
foodList.add(mi);

}
madap.notifyDataSetChanged();
}
}
});


}

我的activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0ead6"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="132dp"
android:text="Please select what you would like to eat"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="172dp"
android:layout_marginTop="70dp"
android:text="Menu."
android:textColor="#000"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="186dp"
android:layout_marginEnd="355dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

我的MenuAdapter.java:

 public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.ViewHolder> {
public List<MenuItem> foodList;
public MenuAdapter(List<MenuItem> foodList){
this.foodList= foodList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_item, parent, false);
return new ViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

holder.mdof.setText(foodList.get(position).getDescof());
holder.mnof.setText(foodList.get(position).getNameof());
holder.mpof.setText(foodList.get(position).getPriceof());

}

@Override
public int getItemCount() {
return foodList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
View mView;
public TextView mnof, mpof, mdof;

public ViewHolder (View itemView){
super(itemView);
mView=itemView;
mnof= mView.findViewById(R.id.nof);
mpof= mView.findViewById(R.id.pof);
mdof= mView.findViewById(R.id.dof);
}}}

最后是 MenuItem.java 类:

public class MenuItem {

public MenuItem(){}
public MenuItem(String nameof, String priceof, String descof) {
this.nameof = nameof;
this.priceof = priceof;
this.descof = descof;
}

public String getNameof() {
return nameof;
}

public void setNameof(String nameof) {
this.nameof = nameof;
}

public String getPriceof() {
return priceof;
}

public void setPriceof(String priceof) {
this.priceof = priceof;
}

public String getDescof() {
return descof;
}

public void setDescof(String descof) {
this.descof = descof;
}

String nameof, priceof,descof;

}

任何有关此的评论都会有帮助。没有错误或任何类型的崩溃或异常。

最佳答案

您需要告诉您的适配器您已使用

在列表中添加数据

yourAdapter.notifyDataSetChanged()

在您的情况下,调用是从 firestore 异步进行的,因此在添加项目的循环完成后添加通知

db.collection("Food").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot doc : task.getResult())
{
MenuItem mi = doc.toObject(MenuItem.class);
foodList.add(mi);

}
madap.notifyDataSetChanged()
Log.d("Firelog","The XML file has issues.");
}
}
});

关于java - Recycler View 不显示从 Firestore 数据库获取的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62185601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com