0

The code below retrieves multiple selected contacts and stores it in the form of ArrayList but once I close the application and open the selected contact list is removed ,so the data should be stored so that once I close and open the app the data remains until the data is removed. Can anyone please help me to store the ArrayList values using shared preference .

 private void chooseContact() {
    Intent intentContactPick = new Intent(MainActivity.this,ContactsPickerActivity.class);
    MainActivity.this.startActivityForResult(intentContactPick,CONTACT_PICK_REQUEST);
}

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CONTACT_PICK_REQUEST && resultCode == RESULT_OK){


        ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");

        String display="";
        for(int i=0;i<selectedContacts.size();i++){

            display += (i+1)+". "+selectedContacts.get(i).toString()+"\n";

        }
        contactsDisplay.setText("Selected Contacts : \n\n"+display);

    }

}

}

the below code is the ArrayList which holds the selected contact values.

 ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");
Meghana
  • 123
  • 1
  • 8

4 Answers4

1

You have different options.

  • Use a service and run it on background so data will not lost on close.
  • Write the data to an object and save it as file and read the object when you need the data.
  • Write object to shared preference.

Convert your array or object to Json with Gson library and store your data as String in json format.

Save;

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPrefs.edit();
Gson gson = new Gson();

String json = gson.toJson(arrayList);

editor.putString(TAG, json);
editor.commit();

Read;

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
Gson gson = new Gson();
String json = sharedPrefs.getString(TAG, null);
Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType();
ArrayList<ArrayObject> arrayList = gson.fromJson(json, type);

use import java.lang.reflect.Type for avoiding errors.

Ranjith KP
  • 858
  • 6
  • 18
  • What does context refer in the above code and how am I supposed to use it in my code above because i am coming cross lot of errors. – Meghana Jan 20 '17 at 11:06
0

See this.

Store

public void storeData(Context context,List<Contact> offlineData){

        SharedPreferences preferences=context.getSharedPreferences("contact_prefs",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();

        Gson gson=new Gson();
        editor.putString("data",gson.toJson(offlineData));
        editor.apply();
    }

Read

 public List<Contact> getData(Context context){
        SharedPreferences preferences=context.getSharedPreferences("contact_prefs",Context.MODE_PRIVATE);
        Gson gson = new Gson();
        return gson.fromJson(preferences.getString("data",null), new TypeToken<List<Photo>>() {
        }.getType());

    }
ak sacha
  • 2,149
  • 14
  • 19
0

Ranjith KP answer is great. dont forget to add dependecy:

compile 'com.google.code.gson:gson:2.8.0'

for the context u can pass ur activity (just use 'this' instead).

TAG - thats just a string, add

String TAG = "tag";

on your activity's members.

Alon Aviram
  • 190
  • 1
  • 15
0

To save a key value pair to the shared preferences :

SharedPreferences sharedPreference = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
Editor editor = sharedPreference.edit();
editor.putString(key, value);
editor.apply();

context : Pass the calling context or Base Context or Application Context (getContext() in Fragments or getBaseContext() in Activity)

Febin K R
  • 886
  • 2
  • 11
  • 21