Reputation: 37
Here is my problem, I want to show all phone numbers in RecyclerView
, how?
databaseReference = firebaseDatabase.getReference("CallLog");
//in on data change method using for
CallLogList logList = snapshot.getValue(CallLogList.class);
list.add(logList);
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map = new HashMap<String, String>(); map.put("phone",phoneNumber);
map.put("name",callName);
map.put("callType",dir);
map.put("date",dateString);
map.put("duration",callDuration+" seconds");
arrayList.add(map);
myRef.push().setValue(arrayList);
Upvotes: 0
Views: 833
Reputation: 2966
After getting the whole list from Firebase iterate them one by one to add them in the list, after the loop set them in the adapter of RecyclerView
, and set the adapter to your desired RecyclerView
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
list = new ArrayList<CallLogList>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
CallLogList logList = snapshot.getValue(CallLogList.class);
list.add(logList);
}
yourAdapter = YourAdapter(list);
yourRecyclerView.setAdapter(yourAdapter);
}
For more info, here is a full example
Note: If you are using LiveData
or Paging
then setting adapters is different, Let me know if you need more example
Upvotes: 1
Reputation: 349
First you should change the tree to be like this:
Call Log
-Calls
--Lhe...
---CallType OUTGOING
--Lhe...
---CallType INCOMING
Next, create "Calls" class in your project with the same fields as in the firebase, so callType, date, duration and phone. After that, with this code, you can get all instances from the database into a list:
public void refreshList(DataSnapshot dataSnapshot) {
List<Class> list = new ArrayList<>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.child("Calls").getChildren())
{
Item value = dataSnapshot1.getValue(Calls.class);
list.add(value);
}
}
Now you have a list of objects of class Calls, from which you can easily access the phone numbers.
Upvotes: 1