如何在Android Studio中为Firebase数据库创建递增数字字符串模型并读取
Hey there! Let's work through your Firebase structure and adapter question step by step—since you're new to FirebaseRecyclerAdapter, I'll break this down clearly and keep it straightforward.
首先,理清你的数据结构
From your description, your Firebase Database structure looks like this:
Food └── uid1 ├── 1: { name: "xxx", value: "xxx" } ├── 2: { name: "xxx", value: "xxx" } └── 3: { name: "xxx", value: "xxx" } └── uid2 ├── 1: { name: "xxx", value: "xxx" } └── ...
The key thing here is that those 1, 2, 3 values are node keys (strings), not fields inside the ingredient data. You don't need to store them in your Ingredients model—we can fetch them directly from Firebase in the adapter.
步骤1:调整你的Ingredients模型类
Your current model only has a x field, but your Firebase nodes contain name and value data. Let's update the model to match your actual data structure:
package Model; public class Ingredients { // 匹配Firebase节点中的字段 public String name; public String value; // Firebase需要空构造函数才能反序列化数据 public Ingredients() {} // 带参数的构造函数(可选,用于手动创建实例) public Ingredients(String name, String value) { this.name = name; this.value = value; } // Getter方法,供Adapter读取数据 public String getName() { return name; } public String getValue() { return value; } // Setter方法(可选,用于修改数据) public void setName(String name) { this.name = name; } public void setValue(String value) { this.value = value; } }
步骤2:更新ViewHolder和Adapter逻辑
We'll modify the IngredientsViewHolder to display the node key (your "1", "2", "3" string) along with the ingredient name and value. Then, in the adapter, we'll fetch that key directly from Firebase.
更新ViewHolder
public static class IngredientsViewHolder extends RecyclerView.ViewHolder { View mView; public IngredientsViewHolder(View itemView) { super(itemView); mView = itemView; } // 自定义方法:设置要显示的内容,包含序号、名称、值 public void setIngredientContent(String index, String name, String value) { TextView ingredientsTextView = mView.findViewById(R.id.txt_ingredients); // 格式化显示,比如:"1 - 鸡蛋: 2个" ingredientsTextView.setText(String.format("%s - %s: %s", index, name, value)); } }
更新FirebaseRecyclerAdapter的populateViewHolder
Use getRef(position).getKey() to fetch the node's key (the "1", "2" string) and pass it to the ViewHolder:
@Override public void onStart() { super.onStart(); // 确保数据库引用指向正确路径:Food -> 当前用户的uid String currentUid = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Food").child(currentUid); FirebaseRecyclerAdapter<Ingredients, IngredientsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Ingredients, IngredientsViewHolder>( Ingredients.class, R.layout.list_single_ingredients, IngredientsViewHolder.class, mDatabase ) { @Override protected void populateViewHolder(IngredientsViewHolder viewHolder, Ingredients model, int position) { // 获取当前节点的key(就是"1", "2", "3"这类字符串) String ingredientIndex = getRef(position).getKey(); // 传递数据到ViewHolder显示 viewHolder.setIngredientContent(ingredientIndex, model.getName(), model.getValue()); } }; Ingredients_list.setAdapter(firebaseRecyclerAdapter); }
处理序号排序的小细节
If you ever have more than 9 ingredients, Firebase's default orderByKey() will sort strings lexicographically—meaning "10" would come before "2". To fix this, add an index field (numeric type) to each Firebase node:
1: { index: 1, name: "鸡蛋", value: "2个" }, 10: { index: 10, name: "牛奶", value: "1盒" }
Then update your database reference to sort by this index field:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() .child("Food").child(currentUid) .orderByChild("index");
This ensures proper numeric sorting regardless of how many ingredients you have.
为什么不把序号存在模型里?
Storing the 1, 2 string in your Ingredients model would be redundant—Firebase already stores this as the node key, so fetching it directly is more efficient and avoids data duplication.
内容的提问来源于stack exchange,提问作者Keiren Chow

