gpt4 book ai didi

android - database.DatabaseException : Can't convert object of type java. lang.String 类型

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:17 25 4
gpt4 key购买 nike

我的问题

      08-10 04:23:28.820 21501-21501/my.org.medicare.medicareapp E/AndroidRuntime: FATAL EXCEPTION: main                                              Process: my.org.medicare.medicareapp, PID: 21501                                             com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type my.org.medicare.medicareapp.protest
at com.google.android.gms.internal.zzaln.zzd(Unknown Source)
at com.google.android.gms.internal.zzaln.zzb(Unknown Source)
at com.google.android.gms.internal.zzaln.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at my.org.medicare.medicareapp.FirebaseHelper.fetchData(FirebaseHelper.java:50)
at my.org.medicare.medicareapp.FirebaseHelper.access$000(FirebaseHelper.java:14) atmy.org.medicare.medicareapp.FirebaseHelper$1.onChildAdded(FirebaseHelper.java60)at com.google.android.gms.internal.zzahe.zza(Unknown Source) at com.google.android.gms.internal.zzaje.zzcta(Unknown Source) at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的主要 Activity 代码:

 rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
//INITIALIZE FIREBASE DB
db = FirebaseDatabase.getInstance().getReference().child("Pro");;

helper = new FirebaseHelper(db);
//ADAPTER
adapter = new MyAdapter(this, helper.retrieve());
rv.setAdapter(adapter);

还有我的 FirebaseHelper:

public class FirebaseHelper {
DatabaseReference db;
Boolean saved=null;
ArrayList<protest> spacecrafts=new ArrayList<>();

public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//WRITE IF NOT NULL
public Boolean save(protest spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Pro").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}

//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
//dataSnapshot.getChildren()
for (DataSnapshot ds : dataSnapshot.getChildren())
{
protest spacecraft=ds.getValue(protest.class);
spacecrafts.add(spacecraft);
}
}

//READ THEN RETURN ARRAYLIST
public ArrayList<protest> retrieve() {
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return spacecrafts;
}
}

我的观点持有人

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView nameTxt,propTxt,descTxt;
ItemClickListener itemClickListener;

public MyViewHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
propTxt= (TextView) itemView.findViewById(R.id.propellantTxt);
descTxt= (TextView) itemView.findViewById(R.id.descTxt);
itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener=itemClickListener;
}
@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}

下面是我的数据结构,我试着读“Pro”

Data Structure

我的抗议结构:

public class protest {
private String name;
private String contact;
private String centre;

public protest(){};

public String getName(){return name;}
public String getContact(){return contact;}
public String getCentre(){return centre;}
public void setName(String name) {
this.name = name;
}
public void setContact(String contact) {
this.contact = contact;
}
public void setCentre(String centre) {
this.centre = centre;
}
}

我可以知道这是什么问题吗?我是 firebase 的新手,我需要你的帮助。我在这里读了一些类似的问题,但我仍然无法解决我的问题。这里有专家可以帮助我吗?

更新:再次测试打印..当 Datasnapshot 转到

  public ArrayList<protest> retrieve() {
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
System.out.println(dataSnapshot.getValue());
fetchData(dataSnapshot);
}

08-10 06:26:47.548 26700-26700/my.org.medicare.medicareapp I/System.out: {name=lee, contact=123456, centre=jalan ipoh}

但是当Datasnapshot转到

 private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
//dataSnapshot.getChildren()
for (DataSnapshot ds : dataSnapshot.getChildren())
{
System.out.println(ds.getValue());
protest spacecraft=ds.getValue(protest.class);
spacecrafts.add(spacecraft);
}
}

08-10 06:26:47.548 26700-26700/my.org.medicare.medicareapp I/System.out: jalan ipoh

问题是什么?

最佳答案

您的 fetchData 函数已经接收 DataSnapshot 作为子节点,但即使在这里您仍然循环遍历子节点,这些子节点将有效地循环各个键(姓名联系人中心)。您应该简单地在 DataSnaphot 上调用 getValue 而无需循环:

private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
protest spacecraft=dataSnapshot.getValue(protest.class);
spacecrafts.add(spacecraft);
}

希望对您有所帮助。

关于android - database.DatabaseException : Can't convert object of type java. lang.String 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38864428/

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