如何仅从Firebase获取密钥并将其放入recyclerview

时间:2018-11-18 05:09:55

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

我具有此数据结构来创建总线时间表并提供时序。我不确定如何获取钥匙以获取时间

  "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 Recycler适配器创建仅包含String timeText的类,并将其放入recyclerview。但是有一个错误 错误提示未连接适配器:跳过布局

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

1 个答案:

答案 0 :(得分:0)

要从 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向您发送包装在DataSnapshot对象中的数据。在addListenerForSingleValueEvent()参考上调用fromAPU之后,Firebase将向您发送apuToVistaSnapshot,其中包括“ Apu-to-vista”节点的所有数据。

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