gpt4 book ai didi

android - FirebaseRecyclerAdapter 数据库异常

转载 作者:行者123 更新时间:2023-11-29 23:05:03 24 4
gpt4 key购买 nike

我为单个项目创建了一个模型类和布局,然后创建了 View 持有者和 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);



}
}

FIREBASE DATA STRUCTURE IMAGE

并且请不要让我引用此网站上的其他答案,因为我搜索了 4 个小时并尝试了所有方法但没有成功

最佳答案

你能检查一下psRef的内容吗?

我知道它以 String 的形式出现并且预期是一个对象。

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

关于android - FirebaseRecyclerAdapter 数据库异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56648662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com