I am currently trying to develop an app that serves as a shopping list, in which the user enters text into an EditText, presses a Button, which then saves the value into a List, which then updates a ListView with its value. The app works great, except when the user exits the app, in which case all of the values that the user enters are not saved. This is what I have tried so far to store the List.
public class MainActivity extends ListActivity {
private static final String SHARED_PREFS_NAME = "MY_SHARED_PREF";
ArrayList<String> mylist = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(android.R.id.list);
Button btn = (Button) findViewById(R.id.button);
mylist = (ArrayList<String>) getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.edittext);
mylist.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
listview.setAdapter(adapter);
}
public boolean saveArray() {
SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor mEdit1 = sp.edit();
Set<String> set = new HashSet<String>();
set.addAll(mylist);
mEdit1.putStringSet("list", set);
return mEdit1.commit();
}
public List<String> getArray() {
SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
Set<String> set = sp.getStringSet("list", new HashSet<String>());
return new ArrayList<String>(set);
}
}
and in my .xml layout file I have
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edittext"
android:text="Button" />
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button" >
</ListView>
</RelativeLayout>
I have it so that the values can be entered into the ListView correctly, but the ListView does not save its values after the app is exited out of and reopened. Does anyone know why this is occurring?
the solution to your problem is as follows:
I tried and reworked your code also thanks to this link:
public class MainActivity extends ListActivity {
private static final String SHARED_PREFS_NAME = "MY_SHARED_PREF";
ArrayList<String> mylist = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listview = (ListView) findViewById(R.id.list);
Button btn = (Button) findViewById(R.id.button);
//NOTE: acquire array from shared preferences
mylist = getArray();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mylist);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText) findViewById(R.id.edittext);
mylist.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
};
btn.setOnClickListener(listener);
listview.setAdapter(adapter);
}
public boolean saveArray() {
SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor mEdit1 = sp.edit();
Set<String> set = new HashSet<String>();
set.addAll(mylist);
mEdit1.putStringSet("list", set);
return mEdit1.commit();
}
public ArrayList<String> getArray() {
SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
//NOTE: if shared preference is null, the method return empty Hashset and not null
Set<String> set = sp.getStringSet("list", new HashSet<String>());
return new ArrayList<String>(set);
}
}
..and calls SaveArray () before closing the application
public void onStop() {
saveArray();
super.onStop();
}
or, alternatively, save the array every time you add an element (all'onclick button overwrites the shared preference in the current list)
Since the listview you are using not custom, i.e. is a android.R.layout.simple_list_item_1.
I recommend saving the items in the listview as a List and then save it to the SharedPreferences. If you only want to save a string in SharedPreferences, following functions will help convert between List and String & vice versa.
private String converttoStringfromList(List<String> list) {
String convertedString;
convertedString = TextUtils.join(",", list);
return convertedString;
}
private List<String> converttoListfromString(String serialized) {
List<String> convertedList = null;
convertedList = TextUtils.split(serialized, ",");
return convertedList;
}
Updating from discussion in comments: It should also work, more efficiently, to convert between ArrayList<>String, following threads will help how to do this:
Best way to convert an ArrayList to a string
&
How to convert a String into an ArrayList?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With