I have an app where the main screen is the list view, and then the user can click on an add button which takes the user to another activity where he/she can choose values from a spinner, and there's a button on the bottom called "Save", which when clicked saves the values from the spinners into a list item and it takes the user back to the main screen with the list view and the newly created list item.
This is my code:
package viva.inspection.com.inspectionpicker;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import viva.inspection.com.inspectionpicker.R;
public class ListActivity extends Activity {
private static ArrayList<String> inspections = new ArrayList<String>();
private static ArrayAdapter<String> inspectionAdapter;
private static final String s = "inspection list";
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
ArrayList<String> temp = new ArrayList<String>(settings.getStringSet(s, new HashSet<String>(inspections)));
inspections = temp;
ListView inspectionList = (ListView) findViewById(R.id.listView);
inspectionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
inspectionList.setAdapter(inspectionAdapter);
if(getIntent().getStringExtra("spins") != null) {
addItems(getIntent().getStringExtra("spins"));
}
System.out.println("Created");
}
@Override
protected void onPause(){
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putStringSet(s, new HashSet<String>(inspections));
System.out.println("Paused");
editor.commit();
}
@Override
protected void onDestroy() {
super.onDestroy();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putStringSet(s, new HashSet<String>(inspections));
editor.commit();
System.out.println("Destroyed");
}
@Override
protected void onResume() {
super.onResume();
inspections = temp;
ListView inspectionList = (ListView) findViewById(R.id.listView);
inspectionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inspections);
inspectionList.setAdapter(inspectionAdapter);
System.out.println("Resumed");
}
public static void addItems(String s) {
inspections.add(s);
inspectionAdapter.notifyDataSetChanged();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
return true;
case R.id.action_new:
startActivity(new Intent(this, MyActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
Now, I have the saving down, but it only saves one value and I want it to save multiple values. Also, as you can see I have tried implementing the SharedPreferences class to save the values of my list so that when the user comes back to it after removing the app from multitasking, it will still show the values the user had before. However, this isn't working. I have tried using onPause and onResume (as you can see in the code above), but to no avail. I'm really confused as to why this isn't working, because I have logged the values of when the activity is paused/resumed, and I can see the logged values appearing in the console. All I basically want is to save multiple values in the list and not just one, and to have the values saved so that whenever the activity is destroyed, the list view will show the saved values so no data is lost.
Any help is greatly appreciated. Thank-you in advance.
EDIT: I have solved the saving problem, but now I need to be able to save the values of the ListView so that when the user reopens the app, the values will still be there, and the won't disappear.