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:
The saved entry - pushed key screenshot is below:
When you click it, this is how it is viewed by the user.
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) {
}
});
}