为了账号安全,请及时绑定邮箱和手机立即绑定

在Android中更改活动时如何保持ArrayList值

在Android中更改活动时如何保持ArrayList值

梵蒂冈之花 2021-05-11 17:36:32
这是ViewActivitypublic 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);    }}这是InputActivitypublic 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();显示我的流程是在InputActivity中单击保存按钮时,它将在ViewActivity中运行“添加人员”代码,并将数据从表单添加到ArrayList。那是可行的,但是当我使用意图更改活动并回到ViewActivity时,ArrayList数据消失了。我想要的是保留我已经在InputActivity中输入的所有值。希望你明白我的意思:)
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信