0

I am currently working on something in which I should be able to fetch children of a pushed key from Firebase. It should retrieve child record such as:

  • Date
  • Topic
  • More Info

The each time when I save an entry, it also can be viewed in the application. The firebase tree is below:

enter image description here

The saved entry - pushed key screenshot is below:

enter image description here

When you click it, this is how it is viewed by the user.

enter image description here

The problem is, whenever the id is clicked, it just receives the last entry but not the others which is already is in the Firebase.

My code is below:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    mFirebasedatabase=FirebaseDatabase.getInstance().getReference().child("user").child(uid).child("dailyDiary");
    id = mFirebasedatabase.getKey();
    firebaseAuth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();

    mFirebasedatabase.child(id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            List<String> list = new ArrayList<>();
            for  (DataSnapshot datasnap:dataSnapshot.getChildren()) {

                UserDiary userDiary = datasnap.getValue(UserDiary.class);
                String date = userDiary.getDate_time();
                String info = userDiary.getMoreInfo();
                String topic = userDiary.getTopic();
                Log.d("TAG", date + " / " +  topic +  " / " + info);
                list.add(date + topic + info);
            }
            ListView listView = (ListView) findViewById(R.id.list_diary);
            ArrayAdapter arrayAdapter = new ArrayAdapter<String>(HomeThreeViewDiary.this, android.R.layout.simple_list_item_1, list);
            listView.setAdapter(arrayAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

1 Answers1

0

This is happening because you are setting those values dttime, info and Topic to the corresponding text views on each iteration of your for loop. So you are ending up having a single element, the last element set to the TextView. To solve this, you have to choices, one would be to append those value to the text viws or a more elegant way, would be to use a ListView or a RecyclerView and an adapter. This is how you can retrieve data from a Firebase Realtime database and display it in a ListView using and ArrayAdapter and this is how you can retrieve data and display it in a RecyclerView using FirebaseRecyclerAdapter.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Would you please explain more of Appending Text View? I have done some surfing. i could not find a proper example – Roshan Zaid May 26 '18 at 10:23
  • You can take a look here. – Alex Mamo May 26 '18 at 10:28
  • Yes, it pulls all the data in a single TextView. But again a problem, when you click an id from the list view, it should take just one entry. rather this displays the whole entry for every tap of the list. – Roshan Zaid May 26 '18 at 11:17
  • In this case you need to use the second part of my answer. You cannot achieve this using append method. – Alex Mamo May 26 '18 at 11:30
  • Thanks for the Quick reply. i am on it. – Roshan Zaid May 26 '18 at 11:36
  • this is the error i get from logcat : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.example.daunte_pc.anique.HomeThreeViewDiary$1.onDataChange(HomeThreeViewDiary.java:106) and the line is: listView.setAdapter(arrayAdapter); – Roshan Zaid May 27 '18 at 08:48
  • Please edit your question by adding the code you've written. – Alex Mamo May 27 '18 at 08:51