如何将字符串放入数组中

我从数据库中获取价格


 if (dataSnapshot.child("Price").getValue() != null) {

    Price = String.valueOf(dataSnapshot.child("Price").getValue().toString());

    Log.d("Reservation", "c" + Price);

 }

价格的价值根据服务而增加。如何将这些价格值添加到数组中{20, 30 , 50}


扬帆大鱼
浏览 94回答 2
2回答

至尊宝的传说

据我了解,您正在 for 循环或某些事件调用中执行上述代码逻辑。在这种情况下,您应该列出一个清单并将每个价格保留在该清单中声明ArrayList一些循环或方法之外的地方ArrayList<String> priceList = new ArrayList();if (dataSnapshot.child("Price").getValue() != null) {&nbsp; &nbsp; priceList.add(dataSnapshot.child("Price").getValue().toString());&nbsp;}现在您已经有了priceList{20,10,30} 等值,并且可以在任何地方使用它。

慕桂英546537

如果您不知道数据库中 Price 的元素数量,则ArrayList最好的解决方案是List 接口的可调整大小的数组实现。创建一个ArrayListArrayList<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);           ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java