2

I want to save values in ArrayList with SharedPreference. Then, I want to call values in ArrayList from another class, but it is not saving. How can I do this? With SharedPreferences? To save File? Or create Sqlite? Thank you for helping.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button saveButton = (Button)findViewById(R.id.button);
    final Button delButton = (Button)findViewById(R.id.delButton);
    final ListView listView = (ListView)findViewById(R.id.listView);
    final EditText editText = (EditText)findViewById(R.id.editText);
    final ArrayList<String> arrayList = new ArrayList<String>();

    SharedPreferences sharedPref = this.getPreferences(Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = this.getPreferences(Activity.MODE_PRIVATE).edit();
    editor.putString("numbers", arrayList.toString());
    editor.commit();

    String arrayString = sharedPref.getString("numbers", null);

    final ArrayAdapter<String> arrayAdapter;
    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList);
    listView.setAdapter(arrayAdapter);

    saveButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {
            String str = editText.getText().toString();
            Integer cout = listView.getCount()+ 1;
            String str1 = cout.toString().concat("."+str);
            arrayList.add(listView.getCount(), str1);

            arrayAdapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
            editText.setText(" ");
        }
    });

    delButton.setOnClickListener(new View.OnClickListener()
    {

        public void onClick(View v)
        {
            arrayList.remove(arrayList.size()-1);
            arrayAdapter.notifyDataSetChanged();
        }
    });

}

3 Answers3

0

I think you can not do this editor.putString("numbers", arrayList.toString()); because object.toString() does not convert the ArrayList to String it just make something weird like "@Object3223rw", take a look to this post instead Save ArrayList to SharedPreferences

Community
  • 1
  • 1
Tofasio
  • 425
  • 3
  • 14
0

Save list:

editor.putString("List", TextUtils.join(",", myList));

Get list:

String serialized = sharedPref.getString("List", null);
myList = Arrays.asList(TextUtils.split(serialized, ","));

An observation that works on a List not ArrayList because asList return List. Tofasio's answer does not work if you have duplicate strings that works with set.

kelvincer
  • 5,929
  • 6
  • 27
  • 32
0

My understanding is to use SharedPreference only to save simple key-value pairs (bool, float, int, long and string) and not as a data storage. If you need to save more complex data objects (including lists and arrays), you'll need to serialize/deserialize and save on disk.

http://developer.android.com/guide/topics/data/data-storage.html

mpnap
  • 153
  • 1
  • 1
  • 8