Search code examples
androidandroid-sharedpreferences

Android - How to clear sharepreference by position recyclerview


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:


Solution

  • 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();
        }
    
    }