Android从Firebase获取David数据的索引位置并展示到RecyclerView
获取Firebase排序后数据的索引并展示到RecyclerView
嘿,我完全懂你现在的需求——在按name排序后的Firebase数据列表里,要定位到所有David对应的索引位置,还要把索引和他的note内容一起展示在RecyclerView里。其实核心就是在数据绑定RecyclerView的过程中,跟踪排序后每个item的位置,然后把索引和对应数据关联起来就行,下面给你一步步讲怎么实现:
步骤1:确认Query的正确性
首先你的Query思路是对的,但要注意数据库节点的大小写匹配:你数据库里的节点是Node,但代码里写的是child("node"),这会导致找不到数据,修正后的Query应该是:
Query query = FirebaseDatabase.getInstance().getReference().child("Node").orderByChild("name");
步骤2:准备RecyclerView的Item布局和ViewHolder
先给RecyclerView的每个item准备布局(比如命名为item_note.xml),要包含显示索引和note的TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="16dp"> <TextView android:id="@+id/tv_index" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:textStyle="bold" android:marginEnd="16dp"/> <TextView android:id="@+id/tv_note" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> </LinearLayout>
然后自定义ViewHolder类,用来绑定数据:
public class NoteViewHolder extends RecyclerView.ViewHolder { TextView tvIndex, tvNote; public NoteViewHolder(@NonNull View itemView) { super(itemView); tvIndex = itemView.findViewById(R.id.tv_index); tvNote = itemView.findViewById(R.id.tv_note); } // 绑定索引和note数据,这里用position+1让索引从1开始,也可以直接用position从0开始 public void bindData(String note, int position) { tvIndex.setText(String.valueOf(position + 1)); tvNote.setText(note); } }
步骤3:实现FirebaseRecyclerAdapter并关联索引
FirebaseRecyclerAdapter的onBindViewHolder方法里的position参数,就是排序后列表的索引位置,我们可以直接用它来获取David的位置:
// 先定义数据模型类(下面会补充) FirebaseRecyclerOptions<NoteModel> options = new FirebaseRecyclerOptions.Builder<NoteModel>() .setQuery(query, NoteModel.class) .build(); FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> adapter = new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(options) { @Override protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull NoteModel model) { // 只处理name为David的项,不需要的其他项可以隐藏 if ("David".equals(model.getName())) { holder.bindData(model.getNote(), position); } else { holder.itemView.setVisibility(View.GONE); holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0)); } } @NonNull @Override public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_note, parent, false); return new NoteViewHolder(view); } }; // 给RecyclerView设置适配器并开始监听数据 recyclerView.setAdapter(adapter); adapter.startListening();
补充:NoteModel数据类
Firebase需要一个对应数据结构的模型类,注意必须包含空构造函数:
public class NoteModel { private String name; private String note; // Firebase必须的空构造函数 public NoteModel() {} public NoteModel(String name, String note) { this.name = name; this.note = note; } // Getter方法,Firebase需要通过这些方法读取数据 public String getName() { return name; } public String getNote() { return note; } }
额外小提示
如果你想显示所有数据,但只给David的项标注索引,可以修改onBindViewHolder的逻辑:
@Override protected void onBindViewHolder(@NonNull NoteViewHolder holder, int position, @NonNull NoteModel model) { if ("David".equals(model.getName())) { holder.tvIndex.setText(String.valueOf(position + 1)); holder.tvIndex.setVisibility(View.VISIBLE); } else { holder.tvIndex.setVisibility(View.GONE); } holder.tvNote.setText(model.getNote()); }
内容的提问来源于stack exchange,提问作者Hasan
相关产品推荐
相关产品推荐

