我正在从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异步的,因此在添加项循环完成后添加notify
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.");
}
}
});