2016-10-02 206 views
0

在我的Android應用中,我試圖從我的Firebase後端獲取活動用戶的列表。這些用戶保存在Firebase中,作爲名爲UserActive的Java類包含有關它們的詳細信息。Android Firebase檢索數據

我以爲我做了一切都很好,但我得到一個錯誤,應用程序崩潰。我的代碼:

ChildEventListener activeUserschildEventListener = new ChildEventListener() { 
      @Override 
      public void onChildAdded(DataSnapshot dataSnapshot, String s) { 

       for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) { 

        UserActive uA = userSnapshot.getValue(UserActive.class); 

        Toast.makeText(MapsActivity.this, uA.getUsername(), Toast.LENGTH_LONG).show(); 

       } 

      } 

我的錯誤信息:

FATAL EXCEPTION: main 
Process: shomrim.warpcodes.com.shomrim, PID: 28835 
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type shomrim.warpcodes.com.shomrim.UserActive 
    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 shomrim.warpcodes.com.shomrim.MapsActivity$2.onChildAdded(MapsActivity.java:195) 
    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) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的火力地堡結構是這樣的:

enter image description here

顯然,有一些話我與UserActive失蹤類。如果可以,請你幫助我。謝謝。

我UserActive類:

package shomrim.warpcodes.com.shomrim; 

import android.location.Location; 

/** 
* Created by Gil on 09/18/16. 
*/ 

public class UserActive { 

    String username; 
    Location userlocation; 
    String usericoninmap; 
    String userphonenumber; 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public Location getUserlocation() { 
     return userlocation; 
    } 

    public void setUserlocation(Location userlocation) { 
     this.userlocation = userlocation; 
    } 

    public String getUsericoninmap() { 
     return usericoninmap; 
    } 

    public void setUsericoninmap(String usericoninmap) { 
     this.usericoninmap = usericoninmap; 
    } 

    public String getUserphonenumber() { 
     return userphonenumber; 
    } 

    public void setUserphonenumber(String userphonenumber) { 
     this.userphonenumber = userphonenumber; 
    } 
} 

我databaseref:

groupPosShareRef = mRootRef.child("groups") 
     .child(currentUserGroupsString) 
     .child("groupPosData"); 

的JSON從火力地堡(出口刪除userlocation部分之後:

"groups" : { 
    "-KRietwzwz_5c2oGI0Z8" : { 
     "groupPosData" : { 
     "DDJxiDshfwbJFLxHXnMg7V5fHqy1" : { 
      "username" : "username" 
     } 
     } 

我試圖改變UserActive POJO指向一個不包含Location對象的指針,而不是一個簡單的經緯度字符串。這就是所謂的UsersActive

package shomrim.warpcodes.com.shomrim; 

/** 
* Created by user on 10/02/16. 
*/ 

public class UsersActive { 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getUserlat() { 
     return userlat; 
    } 

    public void setUserlat(String userlat) { 
     this.userlat = userlat; 
    } 

    public String getUserlon() { 
     return userlon; 
    } 

    public void setUserlon(String userlon) { 
     this.userlon = userlon; 
    } 

    public String getUsericoninmap() { 
     return usericoninmap; 
    } 

    public void setUsericoninmap(String usericoninmap) { 
     this.usericoninmap = usericoninmap; 
    } 

    public String getUserphonenumber() { 
     return userphonenumber; 
    } 

    public void setUserphonenumber(String userphonenumber) { 
     this.userphonenumber = userphonenumber; 
    } 

    String username; 
    String userlat; 
    String userlon; 
    String usericoninmap; 
    String userphonenumber; 

} 

還是在同一直線上,並與同樣的錯誤應用程序崩潰:

  UsersActive uA = userSnapshot.getValue(UsersActive.class); 

新的火力地堡JSON:

"groups" : { 
    "-KRietwzwz_5c2oGI0Z8" : { 
     "groupPosData" : { 
     "DDDDDDD" : { 
      "username" : "username1" 
     }, 
     "DDJxiDshfwbJFLxHXnMg7V5fHqy1" : { 
      "username" : "username2" 
     } 
     } 
+0

請發表您的UserActive java代碼 –

+0

謝謝Nishant。現在因爲它不工作,我沒有填寫「字符串usericoninmap,字符串userphonenumber」字符串。但我不認爲這是問題所在。 –

+0

也分享你的數據庫參考添加這個監聽器的地方。我認爲在ref和你的pojo –

回答

4

解決它。

問題是我使用了ChildEventListener,而我應該使用簡單的ValueEventListener。

當我意識到我得到的數據比我所需要的「太深」一步時就能理解。

感謝Nishant的所有幫助!

+0

很高興你意識到有一個數據庫結構缺陷負責這一點。在這裏閱讀更多關於構建數據的信息 - http://stackoverflow.com/questions/39807789/firebase-fan-out-the-most-cost-effective-way/39810725#39810725 –