2 回答

TA貢獻1789條經驗 獲得超10個贊
據我了解,您正在 for 循環或某些事件調用中執行上述代碼邏輯。在這種情況下,您應該列出一個清單并將每個價格保留在該清單中
聲明ArrayList一些循環或方法之外的地方
ArrayList<String> priceList = new ArrayList();
if (dataSnapshot.child("Price").getValue() != null) {
priceList.add(dataSnapshot.child("Price").getValue().toString());
}
現在您已經有了priceList{20,10,30} 等值,并且可以在任何地方使用它。

TA貢獻1848條經驗 獲得超10個贊
如果您不知道數據庫中 Price 的元素數量,則ArrayList
最好的解決方案是
List 接口的可調整大小的數組實現。
創建一個ArrayList
ArrayList<String>?yourList?=?new?ArrayList<String>();
并使用循環添加每個價格
//while there is still elements from Price in database
? ? ? if (dataSnapshot.child("Price").getValue() != null) {
? ? ? ? ? ?Price = String.valueOf(dataSnapshot.child("Price").getValue().toString());
? ? ? ? ? ?yourList.add(Price);
? ? ? ? ? ?...
添加回答
舉報