我为单个项目创建了一个模型类和布局,然后创建了视图持有人和 firebaseRecyclerAdapter 如下代码所示,当我运行时它给了我这个错误
com.google.firebase.database.DatabaseException: Can't
convert object of type java.lang.String to type
com.best.karem.letsplay.Model.PsModel
我尝试添加 onValueEventListener,但它也不起作用
模型类
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(){
}
}
主类
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);
}
}
火碱数据结构图片
请不要向我推荐这个网站上的另一个答案,因为我搜索了 4 个小时并尝试了所有方法都没有奏效
你能检查一下psRef的内容吗?
我知道它以字符串的形式出现,并期望作为一个对象出现。
psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");