ArrayList values storing & retrieving using sharedpreferences

In this blog post, I would like show you how to store & retrieve ArrayList values using shared preferences.

1. Create a new project File -> Android Project. While creating a new project give activity name as and copy this code:


import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {

SharedPreferences sPrefs;
SharedPreferences.Editor sEdit;
List values;
ListView lv;
ArrayAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
lv = (ListView) findViewById(;
values = new ArrayList();
sPrefs = PreferenceManager.getDefaultSharedPreferences(this);

int size = sPrefs.getInt(“size”, 0);
if (size > 0) {
for (int j = 0; j < size; j++) { String name = sPrefs.getString("val" + j, null); values.add(name); addToList(); } } else { addToList(); } } public void onClick(View v) { switch (v.getId()) { case EditText name = (EditText) findViewById(; values.add(name.getText().toString()); addToList(); break; } } private void addToList() { // TODO Auto-generated method stub adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, values); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
for (int i = 0; i < values.size(); i++) { if (i == arg2) { sEdit = sPrefs.edit(); sEdit.remove("val" + i); values.remove(arg2); } } sEdit.putInt("size", values.size()); sEdit.commit(); addToList(); } }); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); sEdit = sPrefs.edit(); sEdit.clear(); for (int i = 0; i < values.size(); i++) { sEdit.putString("val" + i, values.get(i)); } sEdit.putInt("size", values.size()); sEdit.commit(); } } [/sourcecode]   2. Now open your activity_main.xml layout and copy this code: [sourcecode] <RelativeLayout xmlns:android="" xmlns: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" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" > </ListView> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/listView1" android:layout_alignParentTop="true" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_toRightOf="@+id/editText1" android:onClick="onClick" android:text="Add" /> </RelativeLayout> [/sourcecode] That's it. now run the application and try to add some values and onitem click, certain item will be removed from the list. ScreenShot: Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi
  1. surya
    • surya

Leave a Reply

Your email address will not be published. Required fields are marked *