活动1
List<Goods> goodsList = new ArrayList<>();
goodsList = getAllData();
Goods goods = new goods();
intent.putExtra("listbarang", (Serializable) goodsList);
函数
getAllData()
是我的函数,用于显示数据库中的所有数据,并且我通过RecyclerView进行显示。活动2
goodsList = (List<Goods>) getIntent().getSerializableExtra("listbarang");
这就是我获取ArrayList的方法
我想要的只是通过if语句将特定条件附加到ArrayList上。例如,我只想显示库存大于0的商品。
我已经成功存储了ArrayList。但是我想获取该条件的特定数据,也许使用循环或其他方法。
最佳答案
我对您的问题不太了解,我认为您可能需要通过列表中的条件。像这样:
intent.putExtra("listbarang", goodsList);
// Criteria for at least one item in the list
intent.putExtra("criteria", 1);
然后处理额外的内容:
int criteria;
Intent intent = getIntent();
goodsList = (List<Goods>) intent.getSerializableExtra("listbarang");
criteria = intent.getIntExtra("criteria", 0);
// do something based on criteria
for(Goods good: goodsList) {
if(good.getStock >= criteria) {
// process the good.
}
}