【发布时间】:2020-07-12 11:59:58
【问题描述】:
XML
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/texttype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.118"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.132" />
<TextView
android:id="@+id/textfats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/texttype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.118"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.132" />
<TextView
android:id="@+id/textfats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
模型类{
private String Type;
private String Fats;
public productsmodel(){}
public productsmodel(String Type, String Fats){
this.Type = Type;
this.Fats=Fats;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public String getFats() {
return Fats;
}
public void setFats(String fats) {
Fats = fats;
}
}
'
公共类 Products 扩展 AppCompatActivity {
private RecyclerView products_list;
private FirebaseFirestore firebaseFirestore;
private FirestoreRecyclerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
products_list = findViewById(R.id.products_list);
//Query
Query query = firebaseFirestore.collection("Products");
//Recycler options
FirestoreRecyclerOptions<productsmodel> options = new FirestoreRecyclerOptions.Builder<productsmodel>().setQuery(query,productsmodel.class).build();
adapter = new FirestoreRecyclerAdapter<productsmodel, productsviewholder>(options) {
@NonNull
@Override
public productsviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singleitemlist,parent,false);
return new productsviewholder(view);
}
@Override
protected void onBindViewHolder(@NonNull productsviewholder holder, int position, @NonNull productsmodel model) {
holder.Fats.setText(model.getFats()+"");
holder.Type.setText(model.getType()+"");
}
};
products_list.setLayoutManager(new LinearLayoutManager(this));
products_list.setAdapter(adapter);
}
private class productsviewholder extends RecyclerView.ViewHolder {
private TextView Type;
private TextView Fats;
public productsviewholder(@NonNull View itemView) {
super(itemView);
setContentView(R.layout.activity_products);
Type=itemView.findViewById(R.id.textfats);
Fats=itemView.findViewById(R.id.texttype);
}
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}`
-
我的产品活动在我运行后一直崩溃。我在尝试着 在产品页面中添加一个回收器视图,这将使我能够检索 来自云 Firestore 的数据自动进入我的回收站视图。我有 附上与功能和错误相关的所有代码。请 检查一下,谢谢!
https://i.stack.imgur.com/Bs3sn.png(显示错误)
https://i.stack.imgur.com/7ePke.png(模型类)
https://i.stack.imgur.com/k6aAK.png(产品类)
https://i.stack.imgur.com/DeVwZ.png (XML)
【问题讨论】:
-
尝试将您的变量设置为公开的,以便 FB 可以读取和构建它们。
-
感谢兄弟回复!在哪个级别?你能检查一下其他的截图吗?
-
在模型类中
-
你能显示产品类的第 33 行是什么吗?
-
变量始终是私有的。 getter 和 setter 是公开的
标签: java android firebase google-cloud-firestore