Karem Mohamed
Karem Mohamed

Reputation: 379

FirebaseRecyclerAdapter database exception

I created a model class and layout for single item and then created view holder and firebaseRecyclerAdapter as shown in code below and when I run it gives me this error

        com.google.firebase.database.DatabaseException: Can't 
        convert object of type java.lang.String to type 
        com.best.karem.letsplay.Model.PsModel

I tried adding onValueEventListener but it is not working too

Model Class

      public class PsModel {

private String title;
private String location;
private String thumb_image;


public PsModel(String title, String location, String thumb_image) {
    this.title = title;
    this.location = location;
    this.thumb_image = thumb_image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getThumb_image() {
    return thumb_image;
}

public void setThumb_image(String thumb_image) {
    this.thumb_image = thumb_image;
}

public PsModel(){

}


}

Main Class

       public class Ps extends AppCompatActivity {

private Toolbar toolbar;
private FloatingActionButton psBtn;

private RecyclerView recyclerView;
private FirebaseAuth auth;
private DatabaseReference psRef;
private String currentUser;

private FirebaseRecyclerAdapter<PsModel , PsViewHolder> adapter;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ps);

    auth = FirebaseAuth.getInstance();
    currentUser = auth.getCurrentUser().getUid();
    psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");


    recyclerView = findViewById(R.id.ps_recyclerview);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));


    adapter = new FirebaseRecyclerAdapter<PsModel, PsViewHolder>(
            PsModel.class,
            R.layout.list_item,
            PsViewHolder.class,
            psRef
    ) {
        @Override
        protected void populateViewHolder(PsViewHolder viewHolder, PsModel model, int position) {


            viewHolder.itemTitle.setText(model.getTitle());
            viewHolder.itemLocation.setText(model.getLocation());
            Picasso.get().load(model.getThumb_image()).into(viewHolder.itemImage);

        }
    };

    recyclerView.setAdapter(adapter);

}


public static class PsViewHolder extends RecyclerView.ViewHolder{

    public ImageView itemImage;
    public TextView itemTitle , itemLocation;

    public PsViewHolder(@NonNull View itemView) {
        super(itemView);


        itemImage = itemView.findViewById(R.id.item_image);
        itemTitle = itemView.findViewById(R.id.item_title);
        itemLocation = itemView.findViewById(R.id.item_location);



    }
}

FIREBASE DATA STRUCTURE IMAGE

and Please don't refer me to another answer on this site because I searched for this for 4 hours and tried everything and didn't work

Upvotes: 1

Views: 42

Answers (1)

Deˣ
Deˣ

Reputation: 4371

Can you check the content of psRef?

I understand it's coming as String and expected as an object.

 psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");

Upvotes: 1

Related Questions