我被困在我的代碼中使用模型類在列表視圖中檢索Firebase數據。我必須在列表視圖中檢索數據時,我使用字符串類而不是模型類,所以數據顯示,但是當我使用模型類應用程序正在停止和關閉Android的firebase檢索列表視圖中的數據錯誤
這裏是以下給出的錯誤。
Process: com.example.daniyal.audioplayer, PID: 29475
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.daniyal.audioplayer.ModelData
at com.google.android.gms.internal.adw.zzb(Unknown Source)
at com.google.android.gms.internal.adw.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.example.daniyal.audioplayer.Songs$1.onDataChange(Songs.java:44)
at com.google.android.gms.internal.yk.zza(Unknown Source)
at com.google.android.gms.internal.aaf.zzIo(Unknown Source)
at com.google.android.gms.internal.aal.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:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
我的動態檢索數據
DatabaseReference databaseReference;
ListView L1;
List<ModelData> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
L1 = (ListView) findViewById(R.id.AllSongs);
list = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("songs");
}
@Override
protected void onStart() {
super.onStart();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(final DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
ModelData Mod = dataSnapshot1.getValue(ModelData.class);
list.add(Mod);
System.out.println("DATA" + Mod.name);
}
ArrayAdapter adapter = new ArrayAdapter(Songs.this , android.R.layout.simple_list_item_1 , list);
L1.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
這裏是我的ModelClass
public class ModelData {
public String name;
public String link;
public ModelData(){}
public ModelData(String name, String link) {
this.name = name;
this.link = link;
}
}
SEE IMAGE OF FIREBASE STRUCTURE
哎firoz,感謝我明白了。你救了我,只是說「刪除for循環,因爲你直接指向'歌曲'節點」它幫助我,我知道了。 –