Coupling Firebaserecyclerviewadapter to a Boolean/String Map.Entry

Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry

Your value is a Boolean, not a Map. So your comment is correct: you will need to specify Boolean/Boolean.class for the value type. To be able to then look up the key, you will need to upgrade to FirebaseUI-Android 0.2.2. In that release we added a populateViewHolder(VH, T, int) overload that gets the position of the item as a parameter. With that, you can look up the key of the item.

Say this is your JSON structure:

{
"items": {
"pushid1": "Fri Jan 01 2016 16:40:54 GMT-0800 (PST)",
"pushid2": "Fri Jan 01 2016 16:41:07 GMT-0800 (PST)",
"pushid3": "Fri Jan 01 2016 16:41:25 GMT-0800 (PST)",
"pushid4": "Fri Jan 01 2016 16:41:37 GMT-0800 (PST)",
"pushid5": "Fri Jan 01 2016 16:42:04 GMT-0800 (PST)"
},
"index": {
"pushid1": true,
"pushid3": true,
"pushid5": true
}
}

So we store strings representing date/times and have an index to select a subset of these items.

We can now load the nodes from the index, then load the items that those nodes refer to and display them in the views with:

FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder> adapter = 
new FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder>(
Boolean.class, android.R.layout.two_line_list_item, ItemViewHolder.class, ref.child("index")){
protected void populateViewHolder(final ItemViewHolder viewHolder, Boolean model, int position) {
String key = this.getRef(position).getKey();
ref.child("items").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String date = dataSnapshot.getValue(String.class);
((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(date);
}

public void onCancelled(FirebaseError firebaseError) { }
});
}
};

The output on screen:

Android app showing three dates

For the full code, see Activity34559171 in this repo.

How to use a FirebaseRecyclerAdapter with a dynamic reference in Android?

You'll need to join the data, by attaching a addListenerForSingleValueEvent().

FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder> adapter = new FirebaseRecyclerViewAdapter<Boolean, ItemViewHolder>(
Boolean.class, android.R.layout.two_line_list_item, ItemViewHolder.class, friendsRef){
protected void populateViewHolder(final ItemViewHolder viewHolder, Boolean model, int position) {
String key = this.getRef(position).getKey();
ref.child("users").child(key).addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue(String.class);
((TextView)viewHolder.itemView.findViewById(android.R.id.text1)).setText(name);
}

public void onCancelled(FirebaseError firebaseError) { }
});
}
};

I've added an activity Activity36235919 to demonstrate this to my sample repo.

After making it work I realized that I'd answered this before in Coupling FirebaseRecyclerViewAdapter to a Boolean/String Map.Entry, so I'll mark your question as a duplicate.

FirebaseRecyclerAdapter with 2 children

Flattening nested data is not currently a feature of FirebaseUI. If you think there is common demand for it, file a feature request on the Github repo. I haven't seen this request before though, so you might be better off building this on your own fork of the repo.

OR clause in firebase java android

In a NoSQL database you will often have to model your data for the way your app needs it (see this article for a good explanation on NoSQL Data Modeling). So if you need a list of all chats for a specific user, store that list:

/userChats
user1Id
chat1: true
user2Id
chat1: true
chat2: true
user4Id
chat2: true

This process is called denormalizing and it is covered in the article I linked above, in the Firebase documentation on structuring data and in this blog post.



Related Topics



Leave a reply



Submit