【问题标题】:how to get only keys from firebase and put it inside recyclerview如何从firebase只获取密钥并将其放入recyclerview
【发布时间】:2018-11-18 22:51:58
【问题描述】:

我有这个数据结构来创建巴士时刻表并提供时间。我不确定如何获得获得时间的钥匙

  "Schedules" : {
"Apu-to-vista" : {
  "11:00" : "true",
  "12:00" : "true",
  "13:00" : "true",
  "13:30" : "true",
  "14:00" : "true",
  "14:30" : "true",
  "15:00" : "true",
  "15:30" : "true",
  "16:00" : "true",
  "16:30" : "true",
  "17:00" : "true",
  "17:30" : "true",
  "18:00" : "true",
  "18:30" : "true",
  "19:00" : "true",
  "20:30" : "true"
},
"vista-to-apu" : {
  "11:35" : "true",
  "12:00" : "true",
  "13:00" : "true",
  "13:15" : "true",
  "14:00" : "true",
  "14:30" : "true",
  "15:00" : "true",
  "15:30" : "true",
  "16:00" : "true",
  "16:30" : "true",
  "17:00" : "true",
  "17:30" : "true",
  "18:00" : "true",
  "18:30" : "true",
  "19:20" : "true",
  "20:30" : "true"
}
}

如果我这样做,我想得到钥匙mDatabase.getReference("Schedules").child("Apu-to-vista")

这是我迄今为止尝试过的

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.vista, container, false);
    mDatabase = FirebaseDatabase.getInstance();
    fromAPU = mDatabase.getReference("Schedules").child("Apu-to-vista");
    toAPU = mDatabase.getReference("Schedules").child("Vista-to-apu");
    recyclerView = (RecyclerView) view.findViewById(R.id.fromAPURecycler);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(getActivity());
    recyclerView.setLayoutManager(layoutManager);

    adapter = new FirebaseRecyclerAdapter<Schedule, ViewHolder>(
            Schedule.class, R.layout.card_view, ViewHolder.class, toAPU.orderByKey()
    ){
        @Override
        protected void populateViewHolder(ViewHolder viewHolder, Schedule model, int position) {
            viewHolder.mTime.setText(getRef(position).getKey());
        }
    };
    recyclerView.setAdapter(adapter);
    return view;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public final TextView mTime;

    public ViewHolder(View itemView) {
        super(itemView);
        mTime = (TextView) itemView.findViewById(R.id.text_holder);

    }
}

我尝试使用 firebase 回收器适配器创建仅包含 String timeText 的类,并将其放入 recyclerview。但是有一个错误 错误说没有附加适配器:跳过布局

我只是 android 的初学者,有人可以帮助我。谢谢

【问题讨论】:

  • getRef(position).getKey() 返回什么?
  • 我试图记录它,但它没有给我任何东西,因为错误发现没有连接适配器
  • This 是一种推荐的方式,您可以通过这种方式从 Firebase 实时数据库中检索数据并使用 FirebaseRecyclerAdapter 将其显示在 RecyclerView 中。

标签: java android firebase firebase-realtime-database android-recyclerview


【解决方案1】:

Apu-to-vista获取密钥:

fromAPU.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot apuToVistaSnapshot) {
                for (DataSnapshot scheduleSnapshot : apuToVistaSnapshot.getChildren()){
                    String key  = scheduleSnapshot.getKey();
                    // Do whatever you want with this key
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

Firebase 会将包装在 DataSnapshotobject 中的数据发送给您。在fromAPU 参考上调用addListenerForSingleValueEvent() 后,Firebase 将向您发送apuToVistaSnapshot,其中包括“Apu-to-vista”节点的所有数据。

通过在该引用上调用 getChildren(),您可以遍历其子 DataSnapshotinstances 并从每个实例中获取密钥。

【讨论】:

  • 是的,我尝试记录它并且它可以工作,但是我的下一个问题是如何将它放入我的适配器中的回收站视图
  • 如果您已经尝试过并且按照您所说的那样工作,您应该投票将此答案标记为有用(如果不正确)(您应该将其标记为正确)。这就是这个社区的运作方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-02
  • 2017-12-05
  • 2020-05-15
  • 1970-01-01
  • 2016-11-08
  • 2019-10-10
相关资源
最近更新 更多