-1

i have this data structure to create bus schedules and provide the timings. i am not sure on how can i get the keys to get the timing

  "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"
}
}

I want to get the keys if i do mDatabase.getReference("Schedules").child("Apu-to-vista")

this is what i tried so far

    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);

    }
}

ive tried using firebase recycler adapter create the class consist of only String timeText, and putting it into recyclerview. however there is an error the error said no adapter attached: skipping layout

i am just beginner in android, can someone help me. thank you

  • What does `getRef(position).getKey()` return? – Alex Mamo Nov 18 '18 at 11:44
  • i tried to logged it but it doesnt give me anything, since the error catched no adapter attached – Amam Mustofa Nov 18 '18 at 12:54
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Nov 19 '18 at 07:32

1 Answers1

0

To get the keys from 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 sends you the data wrapped in a DataSnapshotobject. After the call of addListenerForSingleValueEvent() on the fromAPU reference Firebase will send you the apuToVistaSnapshot which includes all the data of the "Apu-to-vista" node.

By calling getChildren() on that reference you can iterate through its child DataSnapshotinstances and get the key from each one.

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • yes, i tried to logged it and it works, however my next problem is how to put it into my adapter on recycler view – Amam Mustofa Nov 19 '18 at 04:01
  • If you've tried and it works as you said, you should upvote to mark this answer as useful if not correct (where you should mark it as correct). This is how this community works. – Themelis Nov 19 '18 at 10:49