0
현재 내 firebase DB에서 쿼리를 실행하려고하는데 로그에서 원하는 값을 볼 수 있지만 항상 빈 arraylist를 반환합니다.빈 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를 사용하여 사용자가 만든 모든 트랜잭션을 가져옵니다. Im은 결과를 배열 목록에 저장하려고 시도하지만 배열 목록은 항상 비어 있습니다.
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에이고이 이벤트 구현을 사용해야 할 것 작동하지 않는 경우 :
따라서이 로그는 Log.w (TAG, t.toString()); 값을 반환합니까? –
@ChesterCobus 아니요, 내 POJO의 문자열 표현을 인쇄합니다. – hallaksec
실행 중일 때 POJO의 값을 기록합니까? –