Reputation:
I have 2 queries that pull from the different source (both firebase database realtime) that I would like to appear in my FirebaseRecyclerAdapter. I have been banging my head against this particular issue for a bit but i couldn't think of anything.
How can i achieve this and merge it to the below code.
Likes JSON structure
"Likes" : {
"-LAJRnaAaCGrbv8-cXvD" : {
"5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : {
"-LDqyWmTa-sbgKpxOztv" : {
"Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1"
}
},
"6Zv4wIWV3WOyHxgABXkecK18IJ03" : {
"-LDlj87M--DZVBgBJTUT" : {
"Likes" : "6Zv4wIWV3WOyHxgABXkecK18IJ03"
}
},
"ktLMV4x9kGN43Ggrx5YWhkaG7BL2" : {
"-LDkQ-jAwb7oI3N4T__R" : {
"Likes" : "ktLMV4x9kGN43Ggrx5YWhkaG7BL2"
}
}
}
},
Recipe JSON structure
"PostRecipe" : {
"-LAJRnaAaCGrbv8-cXvD" : {
"appetizer" : "Appetizer",
"cooking_time" : "40 mins",
"desert" : "Dessert",
"difficult_level" : "5",
"main_course" : "Main course",
"name" : "Smokey Party Jollof",
"number_of_people" : "5",
"photo1" : "https://firebasestorage.googleapis.com/v0/b/chefcook-1865d.appspot.com/o/recipe_photos%2F43262?alt=media&token=07103c5d-9924-4e02-985c-fbc02920228f",
"photo2" : "https://firebasestorage.googleapis.com/v0/b/chefcook-1865d.appspot.com/o/recipe_photos%2F43237?alt=media&token=1d398256-0058-4f78-a13f-66301acc4f75",
"photo3" : "something",
"photo4" : "something",
"pizza_fritas" : "Pizza, fritas",
"prepare_time" : "3hrs",
"side_dish" : "Side dish",
"tips" : "Blended stew base is the best base for making traditional stew.",
"userId" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1",
"vegetarian" : "Vegetarian"
}
}
},
MainRecipeFragment.java
...
mDatabase = FirebaseDatabase.getInstance().
getReference().child("Post").child("PostRecipe").orderByChild("photo1").startAt(n);
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mAdapter = new FirebaseRecyclerAdapter<RecipeModel, NewViewHolder>(RecipeModel.class, R.layout
.activty_content_list,
NewViewHolder.class, mDatabase) {
@Override
protected void populateViewHolder(final NewViewHolder viewHolder, final RecipeModel model,
final int
position) {
final DatabaseReference postRef = getRef(position);
Log.e(TAG , "adpter" + model.getName() + model.getPhoto1());
// Set click listener for the whole post view
final String cheeseKey = postRef.getKey();
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Launch PostDetailActivity
Intent intent = new Intent(getActivity(), RecipeDetails.class);
intent.putExtra(RecipeDetails.EXTRA_CHEESE_KEY, cheeseKey);
startActivity(intent);
}
});
// Bind Post to ViewHolder
Context context = getActivity();
viewHolder.bindToCheese(model, context);
}
};
ViewHolder
public class ViewHolder extends RecyclerView.ViewHolder {
String LOG_TAG = ViewHolder.class.getSimpleName();
private View mView;
private ImageView mImageView;
private TextView mTextView;
private static final String TAG = ViewHolder.class.getSimpleName();
public ViewHolder(View view) {
super(view);
mView = view;
mImageView = view.findViewById(R.id.soup_thumbnail);
mTextView = view.findViewById(R.id.soup_title);
//getActivity();
}
public void bindToCheese(Cheeses cheeses, Context c) {
mTextView.setText(cheeses.getTitle());
Log.d(TAG, "title"+ cheeses.getTitle());
Picasso.with(c).load(String.valueOf(cheeses.getImagerUrl())).placeholder(R.mipmap.ic_launcher).fit().into(mImageView);
Log.e(LOG_TAG, "imageurl"+ cheeses.getImagerUrl() + " title" + cheeses.getTitle()) ;
//Here, how do i reach Like and extract information inside.
//I tried the below log, but it is returning null
Log.e(LOG_TAG, "likes"+ cheeses.getLikes());
}
Upvotes: 1
Views: 472
Reputation: 13129
You are thinking your database in a relational way, just do it easy, instead of looping 2 times and getting 2 queries, you can do it in just 1 approach. (this is why firebase structure allows doing it easy)
For instance, lets say you have recipes and likes , you can have inside recipes the likes of each one and then loop over 1 time into that table, get the likes, get the recipe and display it all in 1 loop in your recyclerview, no need a headache to think about querying 2 different nodes.
So , i would suggest you doing a restructure in your db, place the likes inside each recipe and then just loop 1 time into it.
For example, you have this
"Likes" : {
"-LAJRnaAaCGrbv8-cXvD" : {
"5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : {
"-LDqyWmTa-sbgKpxOztv" : {
"Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1"
}
this like corresponds to this recipe
"PostRecipe" : {
"-LAJRnaAaCGrbv8-cXvD" : {
"appetizer" : "Appetizer",
"cooking_time" : "40 mins",
"desert" : "Dessert",
"difficult_level" : "5",
"main_course" : "Main course",...
so, just add that like inside that pushID
and you can have your recipe and the likes of that recipe inside PostRecipe
, this way you only loop once and with 1 query througt all the recipes
this is what you need to do
"PostRecipe" : {
"-LAJRnaAaCGrbv8-cXvD" : {
"appetizer" : "Appetizer",
"likes":{
"5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : {
"-LDqyWmTa-sbgKpxOztv" : {
"Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1"
}
"cooking_time" : "40 mins",
"desert" : "Dessert",
"difficult_level" : "5",
"main_course" : "Main course",...
and then, with this structure you can get the likes of each recipe with just querying the table 1 time.
in your RecipeModel you should have the setters and getters for all the variables you need, for example name ,appetizers , photo1 and so on, be sure that the name of the variables are the same as in firebase databse.
so, to fetch the data you should do somethin like this
remember, your mRef should start at your UserID level. that is mRef.child("PostRecipe").child(uid).addValue....
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
for(DataSnapshot snap : dataSnapshot.getChildren())
RecipeModel rm = snap.getValue(RecipeModel.class);
String name =rm.getName();
String name =rm.getAppetizer();
//and then just create a new variable likes in your POJO
String likes = rm.getLikes();
//and then do whatever you want with the data, remember that forEach will iterate through all the recipes for each user, and then you can get any field inside your users
}
}catch (Exception ex){
// Toast.makeText(FacultiesActivity.this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(DatabaseError error) {
Toast.makeText(FacultiesActivity.this, "Error", Toast.LENGTH_LONG).
});
Upvotes: 1