Data in recycler view not being uploaded to Firebase - android

So I did some research about this but was unable to solve this problem and every solution online led to me to a new error.
I am currently working on an Events App and relatively new to Android Studio.
I have a RecyclerView in an Admin app that verifies all the Event data uploaded on Firebase by an organizer. The organizer uploads the data such as event_tile, event_desc and event_image. This is stored in Firebase under root "Event". Later the Admin App receives these requests in the form of a recycler view, and has a button to approve them. All the approved events are would be stored in a separate table in Firebase with root "Approved_Events".
I am getting stuck in the approval part. The code is running fine with no errors but no data is being uploaded to my Firebase console.
Here is my Main Activity
public class MainActivity extends AppCompatActivity {
private RecyclerView request_eventList;
private DatabaseReference mRef, aRef;
private Button verify_button;
private TextView request_title, request_desc;
private ImageView request_image;
ProgressDialog progressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = new ProgressDialog(this);
mRef = FirebaseDatabase.getInstance().getReference().child("Event");
setContentView(R.layout.activity_main);
request_eventList = (RecyclerView) findViewById(R.id.request_eventList);
request_eventList.setHasFixedSize(true);
request_eventList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Event, RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Event, RequestViewHolder>(
Event.class,
R.layout.admin_event_row,
RequestViewHolder.class,
mRef
) {
#Override
protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
};
request_eventList.setAdapter(firebaseRecyclerAdapter);
}
public static class RequestViewHolder extends RecyclerView.ViewHolder {
View mView;
public Button verify_button;
DatabaseReference mRef, aRef;
ProgressDialog progressDialog;
public RequestViewHolder(View itemView) {
super(itemView);
mView = itemView;
progressDialog = new ProgressDialog(mView.getContext());
verify_button = (Button) mView.findViewById(R.id.approve_button);
mRef = FirebaseDatabase.getInstance().getReference().child("Event");
aRef = FirebaseDatabase.getInstance().getReference().child("ApprovedEvents");
verify_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map <String, String> map = (Map<String, String>) dataSnapshot.getValue();
String title_val = map.get("title");
String desc_val = map.get("desc");
String image_val = map.get("image");
aRef.child("approved_title").setValue(title_val);
aRef.child("approved_desc").setValue(desc_val);
aRef.child("approved_image").setValue(image_val);
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
public void setTitle(String title) {
TextView request_title = (TextView) mView.findViewById(R.id.request_title);
request_title.setText(title);
}
public void setDesc(String desc) {
TextView request_desc = (TextView) mView.findViewById(R.id.request_desc);
request_desc.setText(desc);
}
public void setImage(Context ctx, String image) {
ImageView request_image = (ImageView) mView.findViewById(R.id.request_image);
Picasso.with(ctx).load(image).into(request_image);
}
}
}
MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.admin.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/request_eventList"
android:clickable="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Event Java Class
public class Event {
private String title, desc, image;
public Event(String title, String desc, String image) {
this.title = title;
this.desc = desc;
this.image = image;
}
public Event(){
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
admin_event_row XML file that will fill the recyclerView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/request_image"
android:src="#drawable/abc_btn_check_material"
android:adjustViewBounds="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/request_title"
android:text="Title will come here "
android:padding="10dp"
android:textStyle="bold"
android:textSize="15dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/request_desc"
android:text="Desc will come here "
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/approve_button"
android:text="Yeah it's Cool "/>
</LinearLayout>
Final App Look

If your Event has more unapproved child, move verify_button.setOnClickListener from Holder to Adapter-populateViewHolder. You have to connect verify_button.setOnClickListener to row in recycleviewer to get values of title, desc and image from clicked item.
protected void populateViewHolder(RequestViewHolder viewHolder, Event model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
final String title = model.getTitle();
final String desc = model.getDesc();
final String image = model.getImage();
final DatabaseReference aRef = FirebaseDatabase.getInstance().getReference();
viewHolder.verify_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String key = aRef.child("ApprovedEvents").push().getKey();
Event event = new Event(title, desc, image );
Map<String, Object> eventValues = event.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/ApprovedEvents/" + key, eventValues);
aRef.updateChildren(childUpdates);
}
});
}
};

Related

My Recycler View is empty even though it contains Data

I have an app that retrieves data from Firebase specifically from Firestore, the thing is the recycler view that shows the data is empty, even though it's working in another fragment with the same logic, I don't know where i am wrong or mistaken! And as i said the fragment is totally empty, besides if i added a TextView or Button it shows normally but the recycler view doesn't show up.
ps: when i copied the same code i changed the layout name and the recycler name to point to the other layout and recycler view of the fragment;
Here is the Fragment
FavoriteFragment.java: the fragment that handles the code.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
recyclerView = view.findViewById(R.id.fav_recycler);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
mAuth = FirebaseAuth.getInstance();
FirebaseUser auth_user = mAuth.getCurrentUser();
db = FirebaseFirestore.getInstance();
//Fetch Users Info
Query query = db.collection("posts")
.orderBy("posttime", Query.Direction.DESCENDING); // order the query by date
FirestoreRecyclerOptions<Posts> response = new FirestoreRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class)
.build();
adapter = new MainAdapter(response);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
return view;
}
fragment_favorite.xml: the layout of the fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FavoriteFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/fav_recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
and
MainAdapter.java: The adapter of the Recycler view, also handles the code that makes an item (in this case a post) to be added or removed from the firestore
public class MainAdapter extends FirestoreRecyclerAdapter<Posts, MainAdapter.ViewHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {#link
* FirestoreRecyclerOptions} for configuration options.
*
* #param options
*/
public MainAdapter(#NonNull FirestoreRecyclerOptions options) {
super(options);
}
private FirebaseFirestore db;
private DocumentReference documentReference;
private FirebaseAuth mAuth;
boolean isthere = false;
boolean isExist = false;
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Posts post) {
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
documentReference = db.collection("users").document(mAuth.getUid());
holder.txtTitle.setText(post.getName());
holder.txtDesc.setText(post.getTitle() + "\n" +post.getDesc() + "\n" + post.getBloodtype() + "\n" + post.getCity() + "\n" + post.getNumber()+ "\n" + post.getDeadline());
Glide.with(holder.image.getContext()).load(post.getImage())
.into(holder.image);
holder.root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Snackbar.make(v, post.getUserid() + "\n" + post.getName() + "\n" +post.getBloodtype() + "\n" + post.getCity(), Snackbar.LENGTH_LONG)
.setAnchorView(R.id.navigation) // Set SnackBar above the BottomNavigationView
.show();
}
});
Posts newPost = new Posts(post.getUserid(), post.getName(), post.getTitle(),post.getDesc(), post.getDeadline(), post.getNumber(),
post.getCity(), post.getBloodtype(), post.getImage(), post.getPosttime(), post.getPostid());
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
// Toast.makeText(buttonView.getContext(), "Added to favorite", Toast.LENGTH_SHORT).show();
if (isExist == false) {
db.collection("users").document(mAuth.getUid()).collection("favorites").document(post.getPostid())
.set(newPost)
.addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(#NonNull Task<Void> task) {
Toast.makeText(buttonView.getContext(), "Succeed \nBoolean: " + isExist, Toast.LENGTH_SHORT).show();
}
});
}
}
else {
// removefromfav(post.getPostid(), buttonView);
documentReference.collection("favorites").document(post.getPostid())
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
public void onSuccess(Void aVoid) {
// Toast.makeText(buttonView.getContext(), "Removed from favorite " + isExist, Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
}
}
});
documentReference.collection("favorites").document(post.getPostid())
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
isExist = task.getResult().exists();
if (isExist == true) {
holder.fav.setChecked(true);
}
}
}
});
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.users_item, parent, false);
return new ViewHolder(view);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout root;
public TextView txtTitle;
public TextView txtDesc;
public ImageView image;
public ToggleButton fav;
public ViewHolder(View itemView) {
super(itemView);
root = itemView.findViewById(R.id.list_root);
txtTitle = (TextView) itemView.findViewById(R.id.list_title);
txtDesc = (TextView) itemView.findViewById(R.id.list_desc);
image = (ImageView)itemView.findViewById(R.id.list_image);
fav = (ToggleButton)itemView.findViewById(R.id.favbutton);
}
}
}
EDIT:
I added the Posts class
Posts.java:
public class Posts {
private String userid;
private String name;
private String title;
private String desc;
private String deadline;
private String number;
private String city;
private String bloodtype;
private String image;
private String posttime;
private String postid;
public Posts(){
}
public Posts(String userid,String name, String title, String desc, String deadline,
String number, String city, String bloodtype, String image, String posttime, String postid){
this.userid = userid;
this.name = name;
this.title = title;
this.desc = desc;
this.deadline = deadline;
this.number = number;
this.city = city;
this.bloodtype = bloodtype;
this.image = image;
this.posttime = posttime;
this.postid = postid;
}
public String getUserid(){
return userid;
}
public String getName(){
return name;
}
public String getTitle(){
return title;
}
public String getDesc(){
return desc;
}
public String getDeadline(){
return deadline;
}
public String getNumber(){
return number;
}
public String getCity(){
return city;
}
public String getBloodtype(){
return bloodtype;
}
public String getImage(){
return image;
}
public String getPosttime(){
return posttime;
}
public String getPostid(){
return postid;
}
}
And a screenshot of posts in firestore bellow :
Well, i figure it out, i missed the onStart and onStop to start and stop listening for the adapter.
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}

Firestore data doesn't show up in RecyclerView

I'm trying to put my firestore data within a recyclerview in Android. The app comes up with no errors, however no data shows up.
public class Diseaselist extends AppCompatActivity {
private TextView textView;
private FirebaseFirestore mDatabaseRef;
private Query mChartsQuery;
private RecyclerView mRecycler;
private FirebaseAuth mAuth;
private FirestoreRecyclerAdapter<Upload, ProductViewHolder> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diseaselist);
RecyclerView recyclerView = findViewById(R.id.goodmeme);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
String useruid = currentUser.getUid();
Query query = rootRef.collection("users").document(useruid).collection("diagnoses")
.orderBy("disease", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<Upload> options = new FirestoreRecyclerOptions.Builder<Upload>()
.setQuery(query, Upload.class)
.build();
adapter = new FirestoreRecyclerAdapter<Upload, ProductViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ProductViewHolder holder, int position, #NonNull Upload productModel) {
holder.setProductName(productModel.getDisease());
}
#NonNull
#Override
public ProductViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View views = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_diseaselist, parent, false);
return new ProductViewHolder(views);
}
};
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
private class ProductViewHolder extends RecyclerView.ViewHolder {
private android.view.View view;
ProductViewHolder(android.view.View itemView) {
super(itemView);
view = itemView;
}
void setProductName(final String productName) {
CardView cview =view.findViewById(R.id.cardview);
textView = view.findViewById(R.id.texty);
textView.setText(productName);
cview.setOnClickListener(new android.view.View.OnClickListener() {
#Override
public void onClick(android.view.View view) {
Toast.makeText(getApplicationContext(), productName, Toast.LENGTH_SHORT).show();
}
});
}
}
Layout File:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".Diseaselist"
tools:orientation="vertical">
<TextView
android:id="#+id/texty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="30sp"/>
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:id="#+id/person_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp" />
<TextView
android:id="#+id/person_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/person_photo"
android:textSize="30sp" />
<TextView
android:id="#+id/person_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/person_name"
android:layout_toRightOf="#+id/person_photo" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/goodmeme"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Firestore structure Firestore Structure Screenshot 2
Upload Class code:
package com.Provendor.Provendor;
import android.os.Parcel;
import android.os.Parcelable;
import java.io.Serializable;
import java.util.Calendar;
public class Upload implements Parcelable {
private String mName;
private String mImageUrl;
private String mdisease;
private String mdate;
private float mconfidence;
#Override
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
#Override
public void writeToParcel(Parcel out, int flags) {
out.writeFloat(mconfidence);
out.writeString(mName);
out.writeString(mImageUrl);
out.writeString(mdisease);
out.writeString(mdate);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Upload> CREATOR = new Parcelable.Creator<Upload>() {
public Upload createFromParcel(Parcel in) {
return new Upload(in);
}
public Upload[] newArray(int size) {
return new Upload[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Upload(Parcel in) {
mconfidence = in.readInt();
mName = in.readString();
mImageUrl = in.readString();
mdisease = in.readString();
mdate = in.readString();
}
public Upload() {
mName= ""; //empty constructor needed
}
public Upload(String name, String imageUrl, String disease, float confidence) {
if (name.trim().equals("")) {
name = "No Name";
}
mdisease=disease;
mdate=java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
mName = name;
mImageUrl = imageUrl;
mconfidence=confidence;
}
public String getName() {
return mName;
}
public String getDisease() {
return mdisease;
}
public float getConfidence() {
return mconfidence;
}
public String getDate() {
return mdate;
}
public void setName(String name) {
mName = name;
}
public void setdate() {
mdate=java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
}
public void setDisease(String disease) {
mdisease = disease;
}
public void setConfidence(float confidence) {
mconfidence = confidence;
}
public String getImageUrl() {
return mImageUrl;
}
public void setImageUrl(String imageUrl) {
mImageUrl = imageUrl;
}
}
I expect the output to produce a list of diseases based on the firestore collection, however the recyclerview is left empty. When attached to debug, no errors come up! Thanks for looking this over!
You aren't getting anything from the database because the name of your fields inside the Upload class are different than the name of the fields that exist in the database. Both should match. To solve this, you either change all the name of your fields inside your Upload class to match the properties that exist in the database or your can use annotations. Because I see that you are using private fields and public getters, you should use the PropertyName annotation only in front of the getter, for instance your getName() getter should look like this:
#PropertyName("name")
public String getName() {
return mName;
}

RecyclerView inside RecyclerViewAdapter does not show any item?

I am trying to create recyclerview inside recyclerview which is retrieving the data from Firebase.
So far, I have two recyclerview adapters. GenreAdapter and AnimeInsideGenreAdapter classes.
I was able to show GenreAdapter data into my HomeFragment, but the AnimeInsideGenreAdapter is not showing any data and I have no idea why. I've been looking for my problem on Google but still not getting any luck.
So here are my classes and layouts:
GenreAdapter.class
public class GenreAdapter extends RecyclerView.Adapter {
private List<Genre> genreList;
private Context context;
private List<Anime> perAnimes;
private RecyclerView rvPerAnime;
private RecyclerView.Adapter adapterPerAnime;
class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {
FontAppCompatTextView genre_id, genre_title;
LinearLayout main;
public ViewHolder(final View itemView) {
super(itemView);
main = itemView.findViewById(R.id.main_layout);
genre_id = itemView.findViewById(R.id.genre_id);
genre_title = itemView.findViewById(R.id.genre_title);
}
}
public GenreAdapter(List<Genre> genreList, Context context) {
this.genreList = genreList;
this.context = context;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_genre, parent, false));
}
public void onBindViewHolder(ViewHolder holder, int position) {
Genre genre = this.genreList.get(position);
holder.genre_id.setText(genre.id);
holder.genre_title.setText(genre.genre);
perAnimes = new ArrayList<>();
DatabaseReference databaseAnime = FirebaseDatabase.getInstance().getReference("anime");
Query query_anime = databaseAnime.orderByChild("genreid").equalTo(genre.id);
query_anime.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
perAnimes.clear();
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Anime anime = snapshot.getValue(Anime.class);
perAnimes.add(anime);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
perAnimes.sort(new Comparator<Anime>() {
#Override
public int compare(Anime compare1, Anime compare2) {
return compare1.getTitle().compareToIgnoreCase(compare2.getTitle());
}
});
}
adapterPerAnime.notifyDataSetChanged();
} else {
perAnimes.clear();
adapterPerAnime.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
rvPerAnime = holder.itemView.findViewById(R.id.rvanime);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvPerAnime.setLayoutManager(linearLayoutManager);
rvPerAnime.setHasFixedSize(true);
adapterPerAnime = new AnimeInsideGenreAdapter(perAnimes, context);
rvPerAnime.setAdapter(adapterPerAnime);
Animation up_from_bottom = AnimationUtils.loadAnimation(context, R.anim.up_from_bottom);
holder.itemView.startAnimation(up_from_bottom);
}
public int getItemCount() {
return this.genreList.size();
}
}
item_genre.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.pixplicity.fontview.FontAppCompatTextView
android:id="#+id/genre_title"
style="#style/FontTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:singleLine="true"
android:text="Genre Anime"
android:textColor="#f59e24"
android:textSize="14dp"
android:textStyle="bold" />
<com.pixplicity.fontview.FontAppCompatTextView
android:id="#+id/genre_id"
style="#style/FontTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="ID Genre"
android:textColor="#7c7c7c"
android:textSize="12dp"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvanime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
</LinearLayout>
AnimeInsideGenreAdapter.class
public class AnimeInsideGenreAdapter extends RecyclerView.Adapter<AnimeInsideGenreAdapter.ViewHolder> {
List<Anime> perAnimes;
private Context context;
private Random mRandom = new Random();
class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {
public FontAppCompatTextView anime_id, anime_title;
public ImageView img_anime;
public LinearLayout main;
public ViewHolder(final View itemView) {
super(itemView);
main = itemView.findViewById(R.id.main_layout);
anime_id = itemView.findViewById(R.id.anime_id);
anime_title = itemView.findViewById(R.id.anime_title);
img_anime = itemView.findViewById(R.id.img_anime);
Animation up_from_bottom = AnimationUtils.loadAnimation(itemView.getContext(), R.anim.up_from_bottom);
itemView.startAnimation(up_from_bottom);
}
}
public AnimeInsideGenreAdapter(List<Anime> perAnimes, Context context) {
this.perAnimes = perAnimes;
this.context = context;
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_anime_inside_genre, parent, false));
}
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Anime perAnime = this.perAnimes.get(position);
holder.anime_id.setText(perAnime.getId());
holder.anime_title.setText(perAnime.getTitle());
Toast.makeText(context, perAnime.toString(), Toast.LENGTH_SHORT).show();
Picasso.with(context).load(perAnime.getImgcover()).fit().into(holder.img_anime);
holder.img_anime.getLayoutParams().width = getRandomIntInRange(270,200);
holder.img_anime.getLayoutParams().height = 250;
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
public int getItemCount() {
return this.perAnimes.size();
}
// Custom method to get a random number between a range
protected int getRandomIntInRange(int max, int min){
return mRandom.nextInt((max-min)+min)+min;
}
}
item_anime_inside_genre.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<com.pixplicity.fontview.FontAppCompatTextView
android:id="#+id/anime_id"
style="#style/FontTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="2"
android:singleLine="true"
android:text="Anime ID"
android:textColor="#f59e24"
android:textSize="14dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/img_anime"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"/>
<com.pixplicity.fontview.FontAppCompatTextView
android:id="#+id/anime_title"
style="#style/FontTextViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Anime Title"
android:textColor="#7c7c7c"
android:textSize="12dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Genre.class
public class Genre {
String id;
String genre;
public Genre() {
}
public Genre(String id, String genre) {
this.id = id;
this.genre = genre;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}}
Anime.class
public class Anime {
String id;
String genreid;
String title;
String description;
String imgcover;
public Anime() {
}
public Anime(String id, String genreid, String title, String description, String imgcover) {
this.id = id;
this.genreid = genreid;
this.title = title;
this.description = description;
this.imgcover = imgcover;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGenreid() {
return genreid;
}
public void setGenreid(String genreid) {
this.genreid = genreid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImgcover() {
return imgcover;
}
public void setImgcover(String imgcover) {
this.imgcover = imgcover;
}}
I need to show the anime list from Firebase inside each genres adapter.
Any help will be much appreciated.
Thank you.

RecyclerView﹕ No adapter attached; skipping layout in fragment

I have been reading the different answers here on StackOverflow and tried to implement their solutions but I am still getting the error:
RecyclerView﹕ No adapter attached; skipping layout,
So I initialize my recycler view in onCreateView in my fragment like this :
public class StatusFragment extends Fragment {
DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<ElectricityClass> list = new ArrayList<ElectricityClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
public StatusFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_status, container, false);
recyclerView = rootView.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();
databaseStatus = FirebaseDatabase.getInstance().getReference().child("Electricity");
databaseStatus.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ElectricityClass electricityClass = dataSnapshot.getValue(ElectricityClass.class);
list.add(electricityClass);
}
recyclerView.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext()));
adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
#Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
return rootView;
}
}
my RecyclerViewAdapter class :
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<ElectricityClass> dataList;
public RecyclerViewAdapter(Context context, List<ElectricityClass> list) {
this.dataList = list;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ElectricityClass studentDetails = dataList.get(position);
holder.StudentNameTextView.setText(studentDetails.getName());
holder.StudentNumberTextView.setText(studentDetails.getType());
}
#Override
public int getItemCount() {
return dataList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView StudentNameTextView;
public TextView StudentNumberTextView;
public ViewHolder(View itemView) {
super(itemView);
StudentNameTextView = itemView.findViewById(R.id.ShowStudentNameTextView);
StudentNumberTextView = itemView.findViewById(R.id.ShowStudentNumberTextView);
}
}
}
My list Electricity Class :
class ElectricityClass {
private String id;
private String email;
private String name;
private String type;
private String detail;
private String location;
private String date;
private String imgurl;
public ElectricityClass() {
// Required empty public constructor
}
public ElectricityClass(String id, String currentUserString, String imageUrl, String nameString, String typeString, String detailString, String locationString, String dateString){
this.id = id;
this.email = currentUserString;
this.name =nameString;
this.type = typeString;
this.detail = detailString;
this.location = locationString;
this.date = dateString;
this.imgurl = imageUrl;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
and here is my The layout in the fragment:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/recyclerView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
And the layout of an item:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
card_view:cardElevation="5dp"
card_view:contentPadding="5dp"
card_view:cardCornerRadius="5dp"
card_view:cardMaxElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ECEFF1"
android:padding="10dp">
<TextView
android:id="#+id/StudentName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Student Name: "
android:textColor="#000"
android:textSize="20dp" />
<TextView
android:id="#+id/ShowStudentNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show Student Name"
android:textColor="#000"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/StudentName"
android:layout_toEndOf="#+id/StudentName"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp" />
<TextView
android:id="#+id/StudentNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Phone Number: "
android:textColor="#000"
android:textSize="20dp"
android:layout_below="#+id/StudentName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/ShowStudentNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Show Number"
android:textColor="#000"
android:textSize="20dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_below="#+id/ShowStudentNameTextView"
android:layout_toRightOf="#+id/StudentNumber"
android:layout_toEndOf="#+id/StudentNumber" />
</RelativeLayout>
</android.support.v7.widget.CardView>
now it shows,
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference
here I define my List2
public class StatusFragment extends Fragment {
DatabaseReference databaseStatus;
ProgressDialog progressDialog;
List<ElectricityClass> list2 = new ArrayList<ElectricityClass>();
List<ElectricityClass> list = new ArrayList<ElectricityClass>();
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
and inside onDataChanged(),
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ElectricityClass electricityClass = dataSnapshot.getValue(ElectricityClass.class);
list2.add(electricityClass);
}
refreshRv((ArrayList<ElectricityClass>) list2);
adapter = new RecyclerViewAdapter(getContext().getApplicationContext(), list);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
progressDialog.dismiss();
}
private void refreshRv(ArrayList<ElectricityClass> list2){
list.clear();
list.addAll(list2);
}
You are doing this wrong setting layout and adapter inside that onDataChange method. Rather create a private function that will instantiate your adapter, will set it to RecyclerView and will set layout manager too. (you may want to define the list from constructor of the adapter also like this list = new ArrayList<>(); )
When you are done with this function, call it in onCreateView method of the fragment, and inside onDataChange just call a refresh function(also private) that will clear your list(or not depending on your behaviour), add all new values and notify your adapter using adapter.notifyDataSetChanged() method.
Hope this will help you :)
EDIT: as someone mentioned in a comment, your error is just telling you that adapter is not set to RecyclerView, probably because onDataChanged() was not called. My explanation from above will solve the problem for sure
EDIT:
Define another list let's say list2, replace list.add(); with list2.add(); inside that for from onDataSetChanged();
Then after for, call this function
private void refreshRv(ArrayList<YourDataType> list2){
list.clear();
list.addAll(list2);
adapter.notifyDataSetChanged();
}
this works for me
#felicity just add this function properly
private void refreshRv(ArrayList<YourDataType> list2)
{
list.clear();
list.addAll(list2);
adapter.notifyDataSetChanged();
}
as lonut J.Bejan said

Getting values from multiple Firebase database references in one cardView

I am developing an app which requires me to get reference from two Firebase database references in one cardview. Below is the model of the cardview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginBottom="60dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
app:srcCompat="#drawable/action_setup"
android:id="#+id/dPicture"/>
<TextView
android:text="Username"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:id="#+id/posted_username"
android:paddingLeft="15dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:elevation="10dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/posted_image"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#color/Orange"
/>
<TextView
android:text="Post Title..."
android:padding="15dp"
android:textSize="16dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/posted_title"/>
<TextView
android:text="Summary..."
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="300"
android:maxLines="5"
android:id="#+id/posted_sharing"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
and this is how the database look like in Firebase
{
"Users" : {
"1" : {
"image" : "http://something.com/something",
"name" : "person 1"
},
"2" : {
"image" : "http://someone.com/someone",
"name" : "person 2"
}
},
"posts" : {
"post 1" : {
"content" : "test content 1",
"dPicture" : "http://something.com/something",
"picture" : "http://postimage.com/postimage",
"title" : "test title 1",
"uid" : 1,
"username" : "person 1"
},
"post 2" : {
"content" : "test content 2",
"dPicture" : "http://someone.com/someone",
"picture" : "http://postimage2.com/postimage2",
"title" : "test title 2",
"uid" : 2,
"username" : "person 2"
}
}
}
And the Activity code is
public class MainActivity extends AppCompatActivity {
private RecyclerView mPostedList;
private LinearLayoutManager mLayoutManager;
private DatabaseReference mDatabaseSharings;
private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference mDatabaseUsers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mDatabaseSharings = FirebaseDatabase.getInstance().getReference().child("posts");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users");
mCurrentUser = mAuth.getCurrentUser();
mDatabaseSharings.keepSynced(true);
final String uid = mCurrentUser.getUid();
//bring user to login activity when auth-state is null
mAuthListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() == null) {
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
}
};
//keep users logged in
mDatabaseUsers.keepSynced(true);
mPostedList = (RecyclerView) findViewById(R.id.posted_list);
mPostedList.setHasFixedSize(true);
mPostedList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
//your adapter
FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder>(
PostedModel.class, R.layout.posted_row, PostedModelViewHolder.class, mDatabaseSharings
) {
#Override
protected void populateViewHolder(PostedModelViewHolder viewHolder, PostedModel model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDPicture(getApplicationContext(), model.getDPicture());
viewHolder.setSummary(model.getSummary());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.setUsername(model.getUsername());
final String post_key = getRef(position).getKey();
viewHolder.mView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
Intent delete_intent = new Intent(MainActivity.this, DeleteActivity.class);
delete_intent.putExtra("blog_id", post_key);
startActivity(delete_intent);
return false;
}
});
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent comment_intent = new Intent (MainActivity.this, CommentActivity.class);
comment_intent.putExtra("blog_id", post_key);
startActivity(comment_intent);
}
});
}
};
mPostedList.setAdapter(firebaseRecyclerAdapter);
mLayoutManager = new LinearLayoutManager(MainActivity.this);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mPostedList.setLayoutManager(mLayoutManager);
}
public static class PostedModelViewHolder extends RecyclerView.ViewHolder{
View mView;
public PostedModelViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.posted_title);
post_title.setText(title);
}
public void setDPicture(Context ctx, String dPicture){
ImageView post_dPicture = (ImageView) mView.findViewById(R.id.dPicture);
Picasso.with(ctx).load(dPicture).into(post_dPicture);
}
public void setSummary (String summary){
TextView post_summary = (TextView)mView.findViewById(R.id.posted_sharing);
post_summary.setText(summary);
}
public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.posted_image);
Picasso.with(ctx).load(image).into(post_image);
}
public void setUsername (String username){
TextView post_username = (TextView)mView.findViewById(R.id.posted_username);
post_username.setText(username);
}
}
}
Of course there is the PostedModel.class which is the getters and setters class for it
public class PostedModel {
private String title;
private String summary;
private String image;
private String dPicture;
private String username;
public PostedModel(){}
public PostedModel(String title, String summary, String image) {
this.title = title;
this.summary = summary;
this.image = image;
this.dPicture = dPicture;
this.username = username;
}
public String getDPicture() {
return dPicture;
}
public void setDPicture(String dPicture) {this.dPicture = dPicture;}
public String getTitle() {
return title;
}
public void setTitle(String title) {this.title = title;}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
Now as you can see I have a posting activity that pass both the username as well as the image value from the "Users" database to the "posts" database child (dPicture). However, when a user update his/her username, only the values from the "Users" database are changed and not from existing "posts"
I figure the most efficient way to do this is to populate the viewholder with data from the two database references, connecting both using the user's unique id which is the key in the "Users" database and as child (uid) in the "posts" database but I don't know how to do it... I am attaching a snapshot of the database screenshot with boxes to illustrate in case I am confusing people with my subpar technical language
So the goal is to get the red boxed data and paired it with green boxed data.
Many thanks for all helps beforehand!
Edit:
I don't know if I am on the right track with this... So after researching I think the right way to do it is using a valueEventListener for the Database that I want to get data from inside the adapter class...
So far I got this
FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PostedModel, PostedModelViewHolder>(
PostedModel.class, R.layout.posted_row, PostedModelViewHolder.class, mDatabaseSharings
) {
//calling the value from the Summary.java class
#Override
protected void populateViewHolder(final PostedModelViewHolder viewHolder, final PostedModel model, final int position) {
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getKey().toString() == mDatabaseSharings.child("uid").toString()){
viewHolder.setUsername(model.getUsername());
viewHolder.setDPicture(getApplicationContext(), model.getDPicture());
}
viewHolder.setTitle(model.getTitle());
viewHolder.setSummary(model.getSummary());
viewHolder.setImage(getApplicationContext(), model.getImage());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
But I am getting the key instead of the value passed haha
Try creating two array list say ArrayList abc,def. Then after you fetch the data from Users node, write assign username and image like abc.username. Right after this, fetch data from Posts node and do the same thing for the rest of the data. Then at the end of both the functions,write def.add(abc); I think this should do the job.

Categories

Resources