2017-03-27 111 views
0

我目前正試圖在我的firebase數據庫上運行查詢,我可以看到我想在我的日誌中的值,但我的方法總是返回一個空數組列表。空ArrayList - Firebase數據檢索

請在下面找到我的代碼:

public static ArrayList<Transaction> getUserTransactions(String userId){ 
    Query userTransactionQuery = mDatabaseReference 
      .child("transactions") 
      .orderByChild("userId") 
      .equalTo(userId); 

    final ArrayList<Transaction> transactionsByUser = new ArrayList<>(); 

    userTransactionQuery.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      for (DataSnapshot transactionSnapshot: dataSnapshot.getChildren()) { 
       // TODO: handle the post 
       Transaction t = transactionSnapshot.getValue(Transaction.class); 
       transactionsByUser.add(t); 
       Log.w(TAG, t.toString()); 
      } 

      Log.i(TAG, "Collected User Transactions"); 
     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 
      // Getting Transaction failed, log a message 
      Log.w(TAG, "loadTransactions:onCancelled", databaseError.toException()); 
      // ... 
     } 
    }); 

    return transactionsByUser; 
} 

我的POJO:

public class Transaction { 
    private String id; 

    private String userId; 

    private String categoryId; 

    private TransactionType transactionType; 

    private String creationDate; 

    private long amount; 

    private String description; 
    /** 
    * No args constructor 
    */ 
    public Transaction() {} 


    /** 
    * Constructor for the Transaction object 
    * @param userId id of the user who made the transaction 
    * @param categoryId ID of the category the transaction belongs under 
    * @param amount amount spent/received 
    * @param transactionType income/expense 
    */ 
    public Transaction(String userId, String categoryId, TransactionType transactionType, long amount, String description) { 
     this.userId = userId; 
     this.categoryId = categoryId; 
     this.transactionType = transactionType; 
     this.amount = amount; 

     this.description = description; 

     //Date last changed will always be set to ServerValue.TIMESTAMP 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     String currentDateTime = dateFormat.format(new Date()); // Find todays date 

     this.creationDate = currentDateTime; 
    } 

    /** 
    * Transaction Id Getter 
    * @return transaction Id 
    */ 
    public String getId() { 
     return id; 
    } 

    /** 
    * Transaction Id Setter 
    * @param id Id to assign to the user 
    */ 
    public void setId(String id) { 
     this.id = id; 
    } 

    /** 
    * Category Id getter 
    * @return category Id under which the transaction belongs 
    */ 
    public String getCategoryId() { 
     return categoryId; 
    } 

    /** 
    * Category Id setter 
    * @param categoryId category Id under which the transaction belongs 
    */ 
    public void setCategoryId(String categoryId) { 
     this.categoryId = categoryId; 
    } 

    /** 
    * Transaction type getter 
    * @return type of the transaction whether its an income or an expense 
    */ 
    public TransactionType getTransactionType() { 
     return transactionType; 
    } 

    /** 
    * Transaction type setter 
    * @param transactionType type of the transaction to set 
    */ 
    public void setTransactionType(TransactionType transactionType) { 
     this.transactionType = transactionType; 
    } 

    /** 
    * Transaction creation date getter 
    * @return creation date of the transaction 
    */ 
    public String getCreationDate() { 
     return creationDate; 
    } 

    /** 
    * Transaction creation date setter 
    * @param creationDate creation date to set to the transaction 
    */ 
    public void setCreationDate(String creationDate) { 
     this.creationDate = creationDate; 
    } 

    /** 
    * Transaction amount getter 
    * @return amount set 
    */ 
    public long getAmount() { 
     return amount; 
    } 

    /** 
    * Transaction amount setter 
    * @param amount amount to set 
    */ 
    public void setAmount(long amount) { 
     this.amount = amount; 
    } 

    /** 
    * User Id getter 
    * @return user id corresponding to the transaction 
    */ 
    public String getUserId() { 
     return userId; 
    } 

    /** 
    * User Id setter 
    * @param userId user id to set to the transaction 
    */ 
    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    /** 
    * Description getter 
    * @return Description corresponding to the transaction 
    */ 
    public String getDescription() { 
     return description; 
    } 
    /** 
    * Description setter 
    * @param description Description to set to the transaction 
    */ 
    public void setDescription(String description) { 
     this.description = description; 
    } 

    /** 
    * Transaction object string representation 
    * @return object represented in a string 
    */ 
    @Override 
    public String toString() { 
     return "Transaction{" + 
       "id='" + id + '\'' + 
       ", userId='" + userId + '\'' + 
       ", categoryId='" + categoryId + '\'' + 
       ", transactionType=" + transactionType + 
       ", creationDate='" + creationDate + '\'' + 
       ", amount=" + amount + 
       '}'; 
    } 
} 

我的JSON樹的樣子:

{ 
    transactions: 
    { 
     transactionId { 
     pojo 
     } 
    }   
} 

簡單:我想要做的獲取用戶使用其用戶ID所做的所有事務,我試圖將結果存儲在數組列表中,但數組列表總是返回空;

+0

所以這個日誌:Log.w(TAG,t.toString());返回一個值? –

+0

@ChesterCobus不,它打印我的POJO的字符串表示形式。 – hallaksec

+0

我的意思是它在運行時是否記錄了POJO的值? –

回答

1

所以這只是一個嘗試回答你的問題:

public static void getUserTransactions(String userId, final ArrayList<Transaction> transactionsByUser){ 
Query userTransactionQuery = mDatabaseReference 
     .child("transactions") 
     .orderByChild("userId") 
     .equalTo(userId); 

userTransactionQuery.addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for (DataSnapshot transactionSnapshot: dataSnapshot.getChildren()) { 
      // TODO: handle the post 
      Transaction t = transactionSnapshot.getValue(Transaction.class); 
      transactionsByUser.add(t); 
      Log.w(TAG, t.toString()); 
     } 

     Log.i(TAG, "Collected User Transactions"); 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) { 
     // Getting Transaction failed, log a message 
     Log.w(TAG, "loadTransactions:onCancelled", databaseError.toException()); 
     // ... 
    } 
}); 
} 

使用

ArrayList<Transaction> transactions = new ArrayList<>(); 

getUserTransactions("{userId}", transactions); 

//check after method call while debugging if transactions is empty 

如果不工作,你將不得不使用這個事件的實現,其中的ArrayList是正在使用。請注意,Firebase查詢是異步的。