[英]How to putextra arraylist<object> with if statement and show it in other activity
List<Goods> goodsList = new ArrayList<>();
goodsList = getAllData();
Goods goods = new goods();
intent.putExtra("listbarang", (Serializable) goodsList);
function getAllData()
is my function to show all data in my database, and I show it with RecyclerView by the way. 函数
getAllData()
是我的函数,用于显示数据库中的所有数据,并且我通过RecyclerView对其进行了显示。
goodsList = (List<Goods>) getIntent().getSerializableExtra("listbarang");
and there's how I get the ArrayList 这是我获取ArrayList的方法
all I want is put extra the ArrayList with the specific condition with if statement. 我想要的只是通过if语句将特定条件附加到ArrayList上。 For example, I want to show only the goodies who have stock more than 0.
例如,我只想显示库存大于0的商品。
I have succeeded to store the ArrayList. 我已经成功存储了ArrayList。 But I want to get specific data for that condition, maybe using looping or something else.
但是我想获取该条件的特定数据,也许使用循环或其他方法。
I don't quite understand with your question, I think you probably need to pass the criteria with the list. 我对您的问题不太了解,我认为您可能需要通过列表中的条件。 Something like this:
像这样:
intent.putExtra("listbarang", goodsList);
// Criteria for at least one item in the list
intent.putExtra("criteria", 1);
Then process the extra: 然后处理额外的内容:
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.
}
}
您可以从Parcelable实现商品类,并使用Bundle.putParcelableArrayList存储列表数据。
Solution for passing arrayList between activities, fragment and storing them in sharedPreferences. 在活动之间传递arrayList的解决方案,分段并将它们存储在sharedPreferences中。
Passing arrayList of Object type from One Activity to another activity.
This solution will also used for passing from one fragment to another fragment
or fragment to activity or activity to fragment.
And this also helps to store arrayList of object type in sharedPreferences.
First added this dependency in build.gradle(app) file
compile 'com.google.code.gson:gson:2.4'
for e.g.:
//first Activity
ArrayList<Goods> list=new ArrayList<>();
Gson gson=new Gson();
String goodList=gson.toJson(list);
Intent intent=new Intent(this,SecondActivity.class);
intent.putString("goods",goodList);
startActivity();
//Second Activity
Gson gson=new Gson();
String goodList=getIntent().getString("goods");
Type type=new TypeToken<ArrayList<Goods>>(){}.getType();
List<Goods> list = new gson.fromJson(goodList,type);
I hope this solution will help you. 希望此解决方案对您有所帮助。 Thanks
谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.