I am building an android app and I am having trouble with an ArrayList. I am using it to store strings and then putting those strings into a list. I can add new items to the ArrayList no bother but if I go to a new activity and go back to this list the items are gone. How can I stop this happening?
Here is the MainListActivity:
public class MainListActivity extends FragmentActivity implements NewListItemDialog.NewListItemDialogListener {
List<String> mListItems = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mListItems);
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent matrixIntent = new Intent(getApplicationContext(), MatrixDetailActivity.class);
startActivity(matrixIntent);
}
});
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Dialog dialogView = ((DialogFragment) dialog).getDialog();
EditText newListItemName = (EditText) dialogView.findViewById(R.id.newListItemName);
mListItems.add(newListItemName.getText().toString());
Toast.makeText(this, newListItemName.getText().toString() + " has been added", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.new_list_item:
showNewListItemDialog();
break;
}
return true;
}
public void showNewListItemDialog() {
FragmentManager fm = getSupportFragmentManager();
NewListItemDialog newListItemDialog = new NewListItemDialog();
newListItemDialog.show(fm, "NewListItemDialog");
}
}
Thanks, John