2017-07-30 66 views
0

我被困在我的代碼中使用模型類在列表視圖中檢索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

回答

0

如果您想使用相同的ModelClass ModelData不是嘗試更換一個行:

更換databaseReference = FirebaseDatabase.getInstance().getReference("songs");

databaseReference = FirebaseDatabase.getInstance().getReference();

OR 修改類似下面的火力地堡代碼:

@Override 
    public void onDataChange(final DataSnapshot dataSnapshot) { 

     list.clear(); 
      // Removed for loop, because you are directly pointing to `songs` node and here you can directly get data by Mapping Your Model class 
      ModelData Mod = dataSnapshot.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(); 

    } 

希望它有幫助!

+1

哎firoz,感謝我明白了。你救了我,只是說「刪除for循環,因爲你直接指向'歌曲'節點」它幫助我,我知道了。 –

0

嘗試以下

databaseReference.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(final DataSnapshot dataSnapshot) { 

      list.clear(); 
      for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){ 
       HashMap<String, Object> task_map = (HashMap<String, Object>) dataSnapshot1.getValue(); 

       list.add(new ModelData(task_map.get("name"), task_map.get("link")); 
       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) { 

     } 
    }); 
+0

嘿Mehual Kabaraia,感謝代碼我會嘗試hashmap現在我剛剛解決了我的問題。謝謝回覆:) –

相關問題