我为单个项目创建了一个模型类和布局,然后创建了视图持有者和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个小时并尝试了所有操作,但没有成功
答案 0 :(得分:1)
可以检查psRef的内容吗?
我知道它以String的形式出现,并且期望作为对象。
psRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(currentUser).child("Ps");