在 Android 中更改 Activity 时如何保持 ArrayList 值 [英] How to Keep ArrayList Value When Changing Activity in Android

查看:17
本文介绍了在 Android 中更改 Activity 时如何保持 ArrayList 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是视图活动

public class ViewActivity extends AppCompatActivity {

    private ListView lvPerson;
    private PersonListAdapter adapter;
    public List<Person> mPersonList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view);

        lvPerson = (ListView)findViewById(R.id.listView);

        //Add new person
        Intent intent = getIntent();
        if(intent.hasExtra("add")){
            String name = intent.getStringExtra("name");
            String email = intent.getStringExtra("email");
            String address = intent.getStringExtra("address");
            mPersonList.add(new Person(name, email, address));
            Toast.makeText(getApplicationContext(), "Data berhasil ditambahkan", Toast.LENGTH_SHORT).show();
        }

        //Init adapter
        adapter = new PersonListAdapter(getApplicationContext(), mPersonList);
        lvPerson.setAdapter(adapter);

    }
}

这是输入活动

public class InputActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
    }

    public void saveButtonClicked(View view){

        EditText name = (EditText) findViewById(R.id.editTextName);
        EditText email = (EditText) findViewById(R.id.editTextEmail);
        EditText address = (EditText) findViewById(R.id.editTextAddress);

        String strName = name.getText().toString();
        String strEmail = email.getText().toString();
        String strAddress = address.getText().toString();

        Intent intent = new Intent(this, ViewActivity.class);
        intent.putExtra("add", true);
        intent.putExtra("name", strName);
        intent.putExtra("email", strEmail);
        intent.putExtra("address", strAddress);
        startActivity(intent);

    }
}

显示我的流程是当在 InputActivity 中单击保存按钮时,它将在 ViewActivity 中运行添加人员"代码并将数据从表单添加到 ArrayList.

Show my flow is when save button clicked in InputActivity, it will run "add person" code in ViewActivity and add data from form to ArrayList.

这是可行的,但是当我使用 Intent 更改 Activity 并返回 ViewActivity 时,ArrayList 数据消失了.

That's work, but when i changing activity using intent and i back to ViewActivity, ArrayList data gone.

我想要的是保留我已经在 InputActivity 中输入的所有值.

What i want is to keep all value that i've already input in InputActivity.

希望你明白我的意思:)

Hope you understand what i mean :)

推荐答案

这里有一些很好的答案,但由于您只希望它在一个应用程序会话期间持续存在,我将添加另一种方法,一个单独的方法来保存数据:

There are some good answers here, but since you only want it to persist during one app session I'll add another approach, a singleton to hold the data:

class DataHolder {
    final List<Person> people = new ArrayList<>;

    private DataHolder() {}

    static DataHolder getInstance() {
        if( instance == null ) {
            instance = new DataHolder();
        }
        return instance;
    }

    private static DataHolder instance;
}

在任一活动(或应用中的任何位置)中,您都可以使用

In either activity (or any place in your app), you can access the person list with

List<Person> ppl = DataHolder.getInstance().people;

例如,您将替换

public List<Person> mPersonList = new ArrayList<>();

// not sure why that was public...
private List<Person> mPersonList = DataHolder.getInstance().people;

它只会持续到您当前的应用会话,但是如果您想在将来保存它们,您可以向 DataHolder 添加一个后端数据库,以便每次添加一个人时它都会自动保存在某个地方.

It will only last for your current app session, but if you wanted to save them in the future you could add a backend database to the DataHolder so that every time you added a person it was automatically saved somewhere.

这篇关于在 Android 中更改 Activity 时如何保持 ArrayList 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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