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

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

问题描述

这是ViewActivity

This is ViewActivity

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);

    }
}

这是InputActivity

This is InputActivity

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.

这是可行的,但是当我使用意图更改活动并返回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中更改活动时如何保持ArrayList值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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