简体   繁体   English

如何放置额外的数组列表 <object> 与if语句并在其他活动中显示

[英]How to putextra arraylist<object> with if statement and show it in other activity

Activiy 1 活动1

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对其进行了显示。

Activity 2 活动2

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.

相关问题 putExtra如何发送到新的活动数组列表<STRING []> - putExtra How send to new Activity Arraylist <STRING []> ArrayList中的对象 <Object> 通过putExtra方法传递给另一个活动后,不能通过contains方法识别 - Object from an ArrayList<Object> is not recognised via the contains method after passed it to another activity via putExtra method 如何从其他活动中获取字符串putExtra和getExra? 像全局变量一样? - How to putExtra and getExra the string from any other activity? like a global variable? 如何将数组列表从活动发送到另一个活动,而不访问另一个活动 - How to send arraylist from activity to another, without visiting that other activity 从对象arraylist获取值并将其显示在另一个活动中 - get value from object arraylist and show it in another activity 将其他数组的 ArrayList 传递给 Activity - Passing an ArrayList of other Arrays to an Activity 改造2,如何在其他活动中访问收到的ArrayList? - Retrofit 2, how can I access received ArrayList in other activity? 如何将迭代的ArrayList作为JSON数组传递给另一个活动,并使其显示? - How to pass an iterated ArrayList to another activity as a JSON Array, and make it show? 如何使用 putExtra 从 Activity 向 Java Class 发送数据? - How to send data by using putExtra from an Activity to a Java Class? Intent.putExtra-如何将字符串发送到下一个活动? - Intent.putExtra - How to send a string to the next activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM