2017-09-01 86 views
0

我是新來的編程,我剛開始做我的第一個Android項目。我一直被困在這個問題上幾個小時,仍然不知道爲什麼它不起作用。以下是數據結構。Android的Firebase檢索數據回到gridView

enter image description here

我想補充的期刊爲各自的用戶,那我實現了addJournal()函數工作。但是當我從數據庫中取回這些日誌到gridView時它崩潰了。

下面是如何添加日誌下面的數據庫:

 firebaseAuth = FirebaseAuth.getInstance(); 
     firebaseUser = firebaseAuth.getCurrentUser(); 
     journalReference=FirebaseDatabase.getInstance().getReference(); 

     String journalName = etJournalName.getText().toString().trim(); 
     String companyName = etCompanyName.getText().toString().trim(); 
     String journalColor = "blankForNow"; 
     String userID = firebaseUser.getUid(); 
     String journalID = journalReference.push().getKey(); 

     //validation 
     if (TextUtils.isEmpty(journalName)){ 
      etJournalName.setError("Journal name must not be empty"); 
     } else if (TextUtils.isEmpty(companyName)){ 
      etCompanyName.setError("Company name must not be empty"); 
     } else { 
      //storing data to the database... 
      Journal journal = new Journal(journalID, userID, journalName, companyName, journalColor); 
      journalReference.child("users") 
        .child(userID) 
        .child("journals") 
        .child(journalID).setValue(journal); 
     } 

這裏就是我如何檢索數據如下:

DatabaseReference journalRef = 
    FirebaseDatabase.getInstance().getReference(); 
    journalRef.child("users") 
    .child(userID) 
    .child("journals").addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
      journalArrayList.clear(); 
      for (DataSnapshot journalDS : dataSnapshot.getChildren()) { 
       Journal journal = journalDS.getValue(Journal.class); 
       journalArrayList.add(journal); 
      } 
      gridViewAdapter = new GridViewAdapter(MainActivity.this, journalArrayList); 
      gridView.setAdapter(gridViewAdapter); 
     } 

     @Override 
     public void onChildChanged(DataSnapshot dataSnapshot, Strin 

有人能幫助我嗎? 在此先感謝

+0

你可以在這裏發佈錯誤日誌嗎? – akhilesh0707

+0

請發佈您的錯誤日誌。提前致謝! – barotia

+0

只是找出我可以找到日誌的位置... – Jamen

回答

1

使用addListenerForSingleValueEvent insted的addChildEventListener

希望這可以幫助你

DatabaseReference journalRef =FirebaseDatabase.getInstance().getReference(); 
journalRef.child("users").child(userID).child("journals").addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     journalArrayList.clear(); 
     for (DataSnapshot journalDS : dataSnapshot.getChildren()) { 
      Journal journal = journalDS.getValue(Journal.class); 
      journalArrayList.add(journal); 
     } 
     gridViewAdapter = new GridViewAdapter(MainActivity.this, journalArrayList); 
     gridView.setAdapter(gridViewAdapter); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
    } 
}); 

編輯

ValueEventListener得到的單束團,所有的數據,其中爲ChildEventListener GET數據逐個記錄爲特定節點。 我建議您嘗試一下。爲每個ValueEventListenerChildEventListener片段檢查行爲

編輯1

你得到NullPointerException異常,只是因爲你沒有初始化journalArrayList列表

初始化列表

journalArrayList=new ArrayList<>() 

編輯2您可以使用map獲得價值解決方案

for (DataSnapshot journalDS : dataSnapshot.getChildren()) { 
    Map<String, String> map = (Map<String, String>) journalDS.getValue(); 
    String journalName = map.get("journalName"); 
    String companyName = map.get("companyName"); 
    String journalColor = map.get("journalColor"); 
    String userID = map.get("userID"); 
    String journalID = map.get("journalID"); 
    Journal journal = new Journal(journalID, userID, journalName, companyName, journalColor); 
    journalArrayList.add(journal); 
} 
+0

致命異常:主 進程:com.cabbage.sdpjournal,PID:5840 java.lang.NullPointerException:嘗試在空對象引用 在com.cabbage.sdpjournal.MainActivity $ 1.onDataChange(MainActivity.java:75) – Jamen

+0

@Jamen上調用虛擬方法'void java.util.ArrayList.clear()'只是因爲你得到了空指針錯誤你不是initiliaze你的journalArrayList.clear(); – akhilesh0707

+0

@ akhilesh0707我解決了它像你說的改變addChildEvent addValueEvent ...謝謝 – Jamen

0

嘗試下面的代碼,使用Get和set方法。

public class Journal { 
    private String journalID; 
    private String userID; 
    private String journalName; 
    private String companyName; 
    private String journalColor; 

    public Journal() { 
    } 

    public Journal(String journalID, String userID, String journalName, String companyName, String journalColor) { 
     this.journalID = journalID; 
     this.userID = userID; 
     this.journalName = journalName; 
     this.companyName = companyName; 
     this.journalColor = journalColor; 
    } 

    public String getJournalID() { 
     return journalID; 
    } 

    public void setJournalID(String journalID) { 
     this.journalID = journalID; 
    } 

    public String getUserID() { 
     return userID; 
    } 

    public void setUserID(String userID) { 
     this.userID = userID; 
    } 

    public String getJournalName() { 
     return journalName; 
    } 

    public void setJournalName(String journalName) { 
     this.journalName = journalName; 
    } 

    public String getCompanyName() { 
     return companyName; 
    } 

    public void setCompanyName(String companyName) { 
     this.companyName = companyName; 
    } 

    public String getJournalColor() { 
     return journalColor; 
    } 

    public void setJournalColor(String journalColor) { 
     this.journalColor = journalColor; 
    } 
} 
0

d /期刊:==> DataSnapshot {鍵= -KsvqM79AsS1bPUgtngB,值= {用戶ID = qAzq0h3E8oaUn09SkG0hWhFfTDT2,journalName = Journals001,journalID = -KsvqM79AsS1bPUgtngB,公司名稱= com001,journalColor = blankForNow}} 09-01 21:20:20.364 30374-30374/com.cabbage.sdpjournal D/Journal:==> DataSnapshot {key = -KsvzWaNFsYX488MLSIK,value = {userID = qAzq0h3E8oaUn09SkG0hWhFfTDT2,journalName = Journal007,journalID = -KsvzWaNFsYX488MLSIK,companyName = com007,journalColor = blankForNow}} 09-01 21:20:20.364 30374-30374/com.cabbage.sdpjournal D/Journal:==> DataSnapshot {key = -KsvzdIrgwFG3DaLXyt1,value = {userID = qAzq0h3E8oaUn09SkG0hWhFfTDT2,journalName = Journal008,journalID = -ks vzdIrgwFG3DaLXyt1,companyName = com008,journalColor = blankForNow}} 09-01 21:20:20.365 30374-30374/com.cabbage。sdpjournal D/Journal:==> DataSnapshot {key = -KswCFyIifHf-smTXxu,value = {userID = qAzq0h3E8oaUn09SkG0hWhFfTDT2,journalName = Journals008,journalID = -KswCFyIifHf - smTXxu,companyName = com008,journalColor = blankForNow}} 09-01 21:20:20.365 30374-30374/com.cabbage.sdpjournal D/Journal:==> DataSnapshot {key = -KswV-O82jgtGOaRr6BK,value = {userID = qAzq0h3E8oaUn09SkG0hWhFfTDT2,journalName = journal009,journalID = -KswV-O82jgtGOaRr6BK,companyName = com009,journalColor = blankForNow}}