将项目添加到 ArrayList<String>在活动之间传递的 [英] Add Items to an ArrayList&lt;String&gt; that has been passed between Activities

查看:30
本文介绍了将项目添加到 ArrayList<String>在活动之间传递的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 StackOverflow 和互联网上环顾四周并尝试了一些东西,但所有这些都给我带来了同样的普遍问题.

I have Looked around here on StackOverflow and the internet and tried a few things but all of them are giving me the same general problem.

我在一个活动中创建了一个 ArrayList,然后通过(另一个活动或其他 2 个活动,具体取决于用户的选择)发送它,并且在每个活动中(包括创建 arraylist 的活动)让用户选择一个一组中的单个按钮.选择按钮后,我有一个侦听器,它创建一个简单的字符串,然后将该字符串添加到 ArrayList,或者至少这是我想要它做的.

I have an ArrayList that I create in one activity and then send it through (another activity or 2 others depending on the users choices) and in each activity (including the one that the arraylist is created in) has the user select a single button from a group. Upon selecting the button I have a listener that creates a simple string and then adds that String to the ArrayList, or at least that's what I want it to do.

  1. 我尝试使用 Serialized 类将相同的列表传递给它需要通过的所有活动
  2. 我尝试在每个类中创建一个新的 ArrayList,从通过 intent.putExtra() 发送然后接收的前一个类中复制一个,以便将其复制到新的数组列表中做同样的事情,直到它进入最终的活动.
  3. 我试图理解 Parcleable 的实现,但对我来说似乎太多了(我现在不擅长这个).
  1. I have tried using Serialized classes to pass the same list through all the activities it needs to go through
  2. I have tried making a new ArrayList in each class, copying the one from the previous class that was sent via an intent.putExtra() and then received so it could be copied into a new arraylist to do the same thing until it gets to the final Activity.
  3. I have tried to make sense of Parcleable implementation but It just seems like to much for me (I'm not to good at this right now).

每当我尝试使用 ArrayList 的 .add() 方法(或 .addAll() 就第二次尝试完成这项工作而言.

All of these have given me the same NullPointerException whenever I try and use the .add() method of the ArrayList (or the .addAll() in terms of the second attempt to get this done.

您必须向初学者提供的任何建议和解释将不胜感激!如果需要我可以放代码!!

Any suggestions along with explanations you would have to give to a beginner would be greatly appreciated! I can put code if needed!!

推荐答案

首先,你应该使用 Parcelable,在这种情况下它更有效率.查看此链接以了解为什么":
https://medium.com/@theblackcat102/passing-object-between-activity-using-gson-7dfa11d74e06

First thing, you should use Parcelable, it is more efficient in this case. Have a look at this link for the "why":
https://medium.com/@theblackcat102/passing-object-between-activity-using-gson-7dfa11d74e06

其次,我会这样做:

public class ActivityOne extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        Intent intent = new Intent(this, ActivityTwo.class);
        ArrayList<Person> strings = new ArrayList<Person>(Arrays.asList(new Person("Bob"),new Person("Dude")));
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(ActivityTwo.PARCELABLE_KEY,strings);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}


public class ActivityTwo extends Activity {
    public static final String PARCELABLE_KEY = "array_key";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        if (getIntent() != null) {
            ArrayList<Person> persons = getIntent().getParcelableArrayListExtra(PARCELABLE_KEY);
            Log.d("test", persons.toString());
        }
    }
}

public class Person implements Parcelable {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Person() {
    }

    public Person(String name) {
        this.name = name;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.name);
    }

    public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    private Person(Parcel in) {
        this.name = in.readString();
    }
}

另外,不要害怕 Parcelables!而作为一个好的开发者,你应该偷懒,然后使用 Parcelable 生成器例如:
http://www.parcelabler.com/

Also, do not be afraid of Parcelables! And as a good developer, you should be lazy, then use a Parcelable generator such as:
http://www.parcelabler.com/

只需复制您的 POJO 类并生成(Android Studio 插件也存在).

Just copy your POJO class(es) and generate (an Android Studio plugin also exists).

最后,不要忘记您不能在 2 个活动之间传递无限数据.您也可以考虑将数据放入数据库.

Finally, don't forget that you cannot pass unlimited data between 2 activities. You could also consider putting your data into a database.

这篇关于将项目添加到 ArrayList<String>在活动之间传递的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆