2

In my firebase database the date is the key value (node) which has its own children. How to retrieve the date into firebase adapter and show it in the listview? I am using the below code but the list is blank.

databaseReference = FirebaseDatabase.getInstance().getReference().child("Lectures").child(post_key);

FirebaseListOptions<String> options = new FirebaseListOptions.Builder<String>()
        .setLayout(R.layout.subject_list_heading)

        .setQuery(databaseReference, String.class)
        .build();

adapter = new FirebaseListAdapter<String>(options) {


    @Override
    protected void populateView(View v, String model, int position) {

        TextView subject_name = v.findViewById(R.id.tv_sub_heading);
        subject_name.setText(model);



    }
};

list_of_date.setAdapter(adapter);

This is the JSON object:

{
  "-LHSdSCzD_tZhzqvSOw_" : {
    "17-7-2018" : {
      "Absent" : "0",
      "Present" : "1",
      "Total_Lectures" : "1"
    }

I want to retrieve the date in the listview. Any help is appreciated.

yaha kaha
  • 65
  • 4

1 Answers1

0

To solve this, please add the following lines of code in the onStart() and onStop() methods.

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    if(adapter != null) {
        adapter.stopListening();
    }
}

So because you are using a listener, you need to start listen for changes. If you are interested, this 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
  • 130,605
  • 17
  • 163
  • 193
  • Hi. I add the onstart ànd onstop methods but now the app is crashing giving the error Failed to convert value of type java.util.Hashmap to String. – yaha kaha Jul 19 '18 at 13:53
  • This is basically another problem. In my personal opinion, without seeing the entire code, is caused because you are trying to read data on wrong reference. In order to follow the rules of this comunity, please post another fresh question, so me and other users can help you. – Alex Mamo Jul 19 '18 at 14:41