I have an Activity which lets the user input names and the save them. Once the add Button is clicked the name gets added to the Array and also shown in a ListView with an option to remove a name.
My question is I am trying to get to the point where if the user leaves the Activity then comes back to the Activity the Array and ListView will have been saved so there is no need to input all the names again. At the moment once the Activity is left and then come back to the Array and ListView are empty.
As you can see at the bottom I have tried to use onSaveInstanceState and onRestoreInstanceState but can’t get them to work can anyone expand on this code?
ArrayList<String> playerList = new ArrayList<String>();
ListView employeeList;
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
final MyAdapter adapter = new MyAdapter(this, R.layout.listview_content, playerList);
ListView employeeList = (ListView) findViewById(R.id.namelistview);
employeeList.setAdapter(adapter);
Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
playerList.add(playername.getText().toString());
adapter.notifyDataSetChanged();
playername.setText("");
}});
Button play = (Button) findViewById(R.id.playnow);
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent( demo.AddRemove.this, demo.PasswActivity.class);
intent.putStringArrayListExtra("KEY", playerList);
startActivity(intent);
}});
}
class MyAdapter extends ArrayAdapter<String>{
private OnClickListener listener;
public MyAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
listener = new OnClickListener(){
public void onClick(View v){
playerList.remove(v.getTag());
notifyDataSetChanged();
};
};
}
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView== null ) convertView = getLayoutInflater().inflate(R.layout.listview_content, null);
TextView myTextView1 = (TextView)convertView.findViewById(R.id.playername);
myTextView1.setText(getItem(position));
Button remove = (Button)convertView.findViewById(R.id.remove);
remove.setTag(getItem(position));
remove.setOnClickListener(listener);
return convertView;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
outState.putStringArrayList("Playlist", playerList );
super.onSaveInstanceState(outState); }
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
playerList = savedInstanceState.getStringArrayList("Playlist");
}
}
Have you try making your playerList as
it will solve your purpose