Issue
I'm creating my first Android app which is a forum app wherein users can post.
I read a lot of the same error, but I can't figure it out to solve my problem. I got the error once I call the method LoadPost(); which fetches the data from Direbase straight to my RecycleView.
Here is my Firebase data structure:
Here's my ViewHolder:
public class MyViewHolder3 extends RecyclerView.ViewHolder {
CircleImageView circleImageView;
TextView user_name, user_power, user_dep, date_posted, user_post_description, upvote_count;
Button upvote_btn, comment_btn;
public MyViewHolder3(@NonNull View itemView) {
super(itemView);
circleImageView = itemView.findViewById(R.id.circleImageView);
user_name = itemView.findViewById(R.id.user_name);
user_power = itemView.findViewById(R.id.user_power);
user_dep = itemView.findViewById(R.id.user_dep);
date_posted = itemView.findViewById(R.id.date_posted);
user_post_description = itemView.findViewById(R.id.user_post_description);
upvote_count = itemView.findViewById(R.id.upvote_count);
upvote_btn = itemView.findViewById(R.id.upvote_btn);
comment_btn = itemView.findViewById(R.id.comment_btn);
}
Here's my Post class:
public class Post {
private String postdescription, userdep, date, username, userpower, userprofileimage;
public Post() {
}
public String getPostdescription() {
return postdescription;
}
public void setPostdescription(String postdescription) {
this.postdescription = postdescription;
}
public String getUserdep() {
return userdep;
}
public void setUserdep(String userdep) {
this.userdep = userdep;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpower() {
return userpower;
}
public void setUserpower(String userpower) {
this.userpower = userpower;
}
public String getUserprofileimage() {
return userprofileimage;
}
public void setUserprofileimage(String userprofileimage) {
this.userprofileimage = userprofileimage;
}
public Post(String postdescription, String userdep, String date, String username, String userpower, String userprofileimage) {
this.postdescription = postdescription;
this.userdep = userdep;
this.date = date;
this.username = username;
this.userpower = userpower;
this.userprofileimage = userprofileimage;
}
Here's my LoadPost() method:
private void LoadPost() {
options = new FirebaseRecyclerOptions.Builder<Post>().setQuery(ForumRef,Post.class).build();
adapter = new FirebaseRecyclerAdapter<Post, MyViewHolder3>(options) {
@Override
protected void onBindViewHolder(@NonNull MyViewHolder3 holder, int position, @NonNull Post model) {
holder.user_post_description.setText(model.getPostdescription());
holder.user_dep.setText(model.getUserdep());
holder.date_posted.setText(model.getDate());
holder.user_name.setText(model.getUsername());
holder.user_power.setText(model.getUserpower());
Picasso.get().load(model.getUserprofileimage()).into(holder.circleImageView);
}
@NonNull
@Override
public MyViewHolder3 onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.all_post_layout2,parent,false);
return new MyViewHolder3(view);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
And here's the error code I got:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.chitchat.Post
Solution
When you're passing the following reference:
ForumRef = FirebaseDatabase.getInstance().getReference.child("Forums").child(PostKey);
To:
.setQuery(ForumRef,Post.class)
It means that the adapter expects to render on the screen Post
objects. If you take a closer look at your database schema, under the post key (1st
) node, you can find a Post
object and two strings, the count
and the forumname
:
See, IUSH...2Mon
, count
, and forumname
are children that exist under the exact same (1st
) node. So when reading the data from the database, the Firebase-UI library tries to map each child under the above reference into an object of type Post
, which is actually not possible since the last two children are strings. To solve this, you have to move those two strings to a different location. As soon as inside that node will only exist Post
objects, then the error will go away.
Answered By - Alex Mamo
Answer Checked By - Pedro (JavaFixing Volunteer)