-2

Here is my code that I got share preference

 private void getAllSharePreference() {

     SharedPreferences sharedPreferences = getContext().getSharedPreferences(SharePreferenceKey.SONG_LIST, Context.MODE_PRIVATE);

     getSongJson = sharedPreferences.getString(SharePreferenceKey.SONG_LIST, "N/A");

     if (!getSongJson.equals("N/A")) {
         Type type = new TypeToken<List<SongRespones.Songs>>() {}.getType();
         songSharePreference = new Gson().fromJson(getSongJson, type);

         adapter.addMoreItem(songSharePreference);
         rvFavorite.setAdapter(adapter);
    }
}

This is my code that I want to clear list in share preference by position recyclerview.

@Override
public void onClickView(final int position, View view) {

    final PopupMenu popupMenu = new PopupMenu(getContext(), view);
    popupMenu.inflate(R.menu.remove_favorite);
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.popup_remove_favorite:
                    songSharePreference.remove(position);
                    adapter.notifyItemChanged(position);
                    break;
            }
            return false;
        }

    });
    popupMenu.show();
}

But I cannot clear share preference. Please help me:

Bruno A. Klein
  • 320
  • 3
  • 12
Vichit
  • 309
  • 1
  • 3
  • 16
  • 1
    are you trying to clear the shared preference or you are trying to remove an item at position from data set? You question is not clear – Swati Aug 17 '17 at 19:37
  • I want to clear both. – Vichit Aug 17 '17 at 19:38
  • After I clear sharepreference, my data set clear too. so what to do? – Vichit Aug 17 '17 at 19:40
  • 1
    But where are you doing so? I only see - songSharePreference.remove(position) Please post your code and also what you have tried to achieve this and what is the error you are encountering! – Swati Aug 17 '17 at 19:40
  • I don't know how to clear it, so posted ask all of you. – Vichit Aug 17 '17 at 19:42
  • https://stackoverflow.com/questions/3687315/deleting-shared-preferences Please google first. Thanks – Swati Aug 17 '17 at 19:45
  • Possible duplicate of [Remove a specific item from a Set in Sharedpreferences](https://stackoverflow.com/questions/35843240/remove-a-specific-item-from-a-set-in-sharedpreferences) – Sufian Aug 17 '17 at 20:35

1 Answers1

0

If u want to clear all data from SharedPreferences, this is the way to clear all data of this "SharePreferenceKey.SONG_LIST".

@Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.popup_remove_favorite:
                songSharePreference.remove(position);
                adapter.notifyItemChanged(position);
                SharedPreferences sharedPreferences = 
                getContext().getSharedPreferences( 
                SharePreferenceKey.SONG_LIST, Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                constants.editor.clear();
                constants.editor.commit();
                break;
        }
        return false;
    }

i suggest u to use file to save and retrieve an object. It's easy to use and handle.

    private void saveDataToFile() {

    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {

    }
    ObjectOutputStream objectOutputStream = null;
    try {
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {

    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.writeObject(yourObject); //which data u want to save
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (objectOutputStream != null) {
            objectOutputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Retrieve data from file

    private void getDataFromFile() {

    FileInputStream fileInputStream = null;
    try {
        fileInputStream = getContext().openFileInput("fileName");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(fileInputStream);
    } catch (IOException |NullPointerException e) {
        e.printStackTrace();
    }
    try {
        yourObject = (ObjectClass) objectInputStream.readObject(); //if arraylist then cast to arrylist ( (ArrayList<ObjectClass>) objectInputStream.readObject();)
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        objectInputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}