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);
...
添加回答
举报