Reputation: 345
I am using FirebaseRecyclerAdapter and I need to show post for uses ids equals true, here's my firebase database:
{
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2"
}
},
"pypD1SYZkbcYesk09WuMUY1AkTf1" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "Ali"
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Ali"
}
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : true,
"pypD1SYZkbcYesk09WuMUY1AkTf1" : true
}
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Profile" : "https://twitter.com/2Top/profile_image?size=original",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Mike",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Mike",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
"-KeD2fBUQ09HVMrvAneb" : {
"Describe" : "ee",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488441999,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "2to7p",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
},
"-KeDfnMWiB7k_4J3FBgZ" : {
"Describe" : "f12g",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488452517,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
"-KeDfrw4inUQFtIDXJHp" : {
"Describe" : "fg",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488452536,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2top",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
}
},
"Users" : {
"1DUK1pHEEoaImX8iOOspEMEWCPm2" : {
"Username" : "Tom"
},
"BQOwNdGAeRXiBnVj7ijY1EkFiDC3" : {
"Username" : "7uthaifah"
},
"ECWHIksxJ0Q5SUlIrev4BjnjmrJ3" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Jice"
},
"I5o3JMpYHwZXQD0t2ogWHCQbrZv1" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Mike"
},
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"Cover" : "http://lucascontabil.com/home/wp-content/uploads/2015/05/city-wallpaper-7-400x200.jpg",
"Profile" : "https://lh5.googleusercontent.com/--IAMp28UlmE/AAAAAAAAAAI/AAAAAAAAAAA/AAomvV2EM8Ut2nW9j0YE2vRZhJyQBHMsrA/s200-c/photo.jpg",
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1",
"Username" : "Dom"
},
"ZxqBgLq4ekUJii05AvV86z0i3mH2" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Jone"
},
"mZ5jjPNLlcc8OguVzWJLAYhavmg1" : {
"Profile" : "https://twitter.com/2Top/profile_image?size=original",
"Username" : "Twitter"
},
"pypD1SYZkbcYesk09WuMUY1AkTf1" : {
"Profile" : "https://lh3.googleusercontent.com/-p1mB5WbUN2s/AAAAAAAAAAI/AAAAAAAAAAo/r3Z4o-VSODQ/s200-c/photo.jpg",
"Username" : "Mike"
},
"z2SNUlLd6mQM8nnlkU2VUws5Ggl2" : {
"Cover" : "http://lucascontabil.com/home/wp-content/uploads/2015/05/city-wallpaper-7-400x200.jpg",
"Email" : "@dd",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "2277top"
}
}
}
I have used this method but it doesn't work:
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
Query q = mDatabaseFriends.orderByKey().equalTo(true);
mQuery = mDatabase.orderByChild("UID").equalTo(q.toString());
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mQuery) {
@Override
protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {}};
I need the query that I use it to show posts if ids = true
Here's my method that I used it but it show the only the first following user:
mDatabaseFriends.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot following : dataSnapshot.getChildren()) {
Query X = mDatabase.orderByChild("UID").equalTo(following.getKey());
FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, X ) {}});
Upvotes: 0
Views: 191
Reputation:
You can achieve what you want by using a regular RecyclerAdapter. With your current database design you won't be able to translate what you want into a single firebase query, so the FirebaseRecyclerAdapter is out.
mDatabaseFriends.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot following : dataSnapshot.getChildren()) {
mDatabase.orderByChild("UID").equalTo(following.getKey()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);
// Here you can add your 'post' to a dataset that you can use to initialize a new RecyclerAdapter
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Another option is to simply change your database design to allow for the use of a FirebaseRecyclerAdapter or a FirebaseIndexRecyclerAdapter (which is more likely). I think you'll need to use Client-side fan-out to achieve this though.
These links should be relevant to what you're trying to achieve.
The Firebase Blog: Client-side fan-out
It's been a while and I can't test the code right now, but let me know if this is working for you.
First the adapter
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PostViewHolder> {
private ArrayList<Getting_Posts> mdataSet;
public RecyclerAdapter(ArrayList<Getting_Posts> dataSet) {
mdataSet = dataSet;
}
@Override
public RecyclerAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_card_design, parent, false);
return new PostViewHolder(inflatedView);
}
@Override
public void onBindViewHolder(RecyclerAdapter.PostViewHolder holder, int position) {
Gettings_Posts post = mdataSet.get(position);
// Set the properties of the holder
// I don't know what properties you used in the viewholder so I'll just give an example
// example: holder.Name.setText(post.getName());
}
@Override
public int getItemCount() {
return mdataSet.size();
}
public void add(int position, Getting_Posts post) {
mdataset.add(position, post);
notifyItemInserted(position);
}
}
Use it in the following way. Declare the following properties in your activity/fragment.
ArrayList<Getting_Posts> posts;
private RecyclerAdapter mAdapter;
private RecyclerView mRecycler;
In the onCreate initialize them like this.
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); // Replace this with the ID of your recyclerView
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new RecyclerAdapter(posts);
mRecycler.setAdapter(mAdapter);
Then add my original solution just replace the following line
Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);
with this
mAdapter.add(mAdapter.mdataSet.size(), dataSnapshot.getValue(Getting_Posts.class));
mAdapter.notifyDataSetChanged();
Like I said, I didn't get any time to test it so there might be some errors, but I wanted to give you something to work with. You should be able to find plenty of documentation on RecyclerView and RecyclerAdapter online to help you with solving this.
Upvotes: 1