gpt4 book ai didi

java - 我需要从 FirebaseDatabse 读取数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:41 26 4
gpt4 key购买 nike

我需要从 firebase 数据库检索数据,但在执行时遇到一些错误,我在下面发布了这些错误以及我正在使用的代码。请仔细检查并通知我哪里做错了。

提前致谢。

这是我的MainActivity.java

public class MainActivity extends AppCompatActivity 
{
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseReference;
private ChildEventListener mChildEventListener;
private ListView mListView;
private CustomAdapter mAdapterClass;


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

mListView=(ListView)findViewById(R.id.mainListView);
List<AdapterClass> list=new ArrayList<>();
mAdapterClass=new CustomAdapter(this,R.layout.item_list,list);
mListView.setAdapter(mAdapterClass);


mFirebaseDatabase=FirebaseDatabase.getInstance();
mDatabaseReference=mFirebaseDatabase.getReference().child("messages");


FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,CreatePost.class);
startActivity(intent);
}
});


mChildEventListener=new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
AdapterClass data = dataSnapshot.getValue(AdapterClass.class);
mAdapterClass.add(data);

}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
};
mDatabaseReference.addChildEventListener(mChildEventListener);


}

}

这是我的CustomAdapter.java

public class CustomAdapter extends ArrayAdapter<AdapterClass> {
public CustomAdapter(@NonNull Context context, int resource, @NonNull List<AdapterClass> objects) {
super(context, resource, objects);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View currentView=convertView;
if(currentView==null)
{
currentView= LayoutInflater.from(getContext()).inflate(R.layout.item_list,parent,false);
}
AdapterClass adapter=getItem(position);
TextView txt=(TextView)currentView.findViewById(R.id.listItemTextView);
txt.setText(adapter.getMessage());
return currentView;
}
}

这是我的AdapterClass.java

public class AdapterClass {

private String mMessage;

public AdapterClass()
{
}
public AdapterClass(String message)
{
this.mMessage=message;
}
public String getMessage()
{
return mMessage;
}
public void setMessage(String message)
{
mMessage=message;
}

}

我得到的错误是

 FATAL EXCEPTION: main
Process: com.example.android.vvietproject, PID: 22972
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android.vvietproject.AdapterClasses.AdapterClass
at com.google.android.gms.internal.zzepg.zzb(Unknown Source)
at com.google.android.gms.internal.zzepg.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.android.vvietproject.MainActivity$2.onChildAdded(MainActivity.java:61)
at com.google.android.gms.internal.zzegg.zza(Unknown Source)
at com.google.android.gms.internal.zzelk.zzcal(Unknown Source)
at com.google.android.gms.internal.zzelq.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6816)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)

应用程序已终止。

最佳答案

问题出在这一行:

 AdapterClass data = dataSnapshot.getValue(AdapterClass.class);

您无法将 firebase 数据转换为 pojo。

您可以做的一件事是:

public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String message = (String)dataSnapshot.child("message").getValue();
AdapterClass data = new Adapter(message);
mAdapterClass.add(data);

}

关于java - 我需要从 FirebaseDatabse 读取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49828589/

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