我正在尝试使用我的Firebase中的数据显示自定义列表视图。我在下面有这个现有代码。构建它时没有任何错误。但是该应用程序停止运行。我认为错误是来自datasnapshot。我想知道我做错了什么。我是android开发的新手。预先感谢
我在MainActivity中有此代码
mRef = FirebaseDatabase.getInstance().getReference().child("Person");
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot){
String sName, sSport, sTown;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
// PeopleGetSet arr = new PeopleGetSet("name", "sport", "town");
sName = (ds.child("name").getValue(PeopleGetSet.class).getName());
sSport = (ds.child("sport").getValue(PeopleGetSet.class).getSport());
sTown = (ds.child("town").getValue(PeopleGetSet.class).getTown());
/* Log.d(TAG, "Show Data name: " + info.getName());
Log.d(TAG, "Show Data sport: " + info.getSport());
Log.d(TAG, "Show Data town: " + info.getTown());*/
PeopleGetSet info = new PeopleGetSet(sName, sSport, sTown);
ArrayList<PeopleGetSet> array = new ArrayList<>();
array.add(info);
ViewDatabase adapter = new ViewDatabase(this, R.layout.adapter_view_layout, array);
mListView.setAdapter(adapter);
}
}
这是我的适配器代码
public class ViewDatabase extends ArrayAdapter<PeopleGetSet> {
private static final String TAG="ViewDatabase";
private Context mContext;
int mResource;
public ViewDatabase(@NonNull Context context, int resource, @NonNull List<PeopleGetSet> objects) {
super(context, resource, objects);
this.mContext=context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
String name =getItem(position).getName();
String sport =getItem(position).getSport();
String town =getItem(position).getTown();
PeopleGetSet person = new PeopleGetSet(name,sport, town);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView=inflater.inflate(mResource, parent, false);
TextView tvName = (TextView)convertView.findViewById(R.id.textView);
TextView tvSport = (TextView)convertView.findViewById(R.id.textView2);
TextView tvTown = (TextView)convertView.findViewById(R.id.textView3);
tvName.setText(name);
tvSport.setText(sport);
tvTown.setText(town);
return convertView;
}
}
这是错误消息
08-17 16:29:22.764 4943-4943/org.pelco3.www.customlistadapter E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.pelco3.www.customlistadapter, PID: 4943
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type org.pelco3.www.customlistadapter.PeopleGetSet
at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source:259)
at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at org.pelco3.www.customlistadapter.MainActivity.showData(MainActivity.java:76)
at org.pelco3.www.customlistadapter.MainActivity.access$100(MainActivity.java:22)
at org.pelco3.www.customlistadapter.MainActivity$2.onDataChange(MainActivity.java:57)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:0)
您将对象解析为字符串,这就是您的应用程序崩溃的原因。 您要做的就是将每个循环的快照传递给模型类,并将其添加到您的数组列表中。
干杯:)
ArrayList<PeopleGetSet> array = new ArrayList<>();
private void showData(DataSnapshot dataSnapshot){
String sName, sSport, sTown;
for(DataSnapshot ds : dataSnapshot.getChildren()) {
PeopleGetSet arr = ds.setvalue(PeopleGetSet.class);
//Object obj= ds.setvalue(Object.class);
array.add(info);
}
ViewDatabase adapter = new ViewDatabase(this, R.layout.adapter_view_layout, array);
mListView.setAdapter(adapter);
}