Android - 如何通过位置recyclerview清除共享偏好

时间:2017-08-17 19:14:22

标签: android android-sharedpreferences

这是我的代码,我有共享偏好

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

这是我的代码,我希望通过位置recyclelerview清除共享偏好中的列表。

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

但我无法明确分享偏好。 请帮帮我:

1 个答案:

答案 0 :(得分:0)

如果您想清除SharedPreferences中的所有数据,这是清除此“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;
    }

我建议您使用文件来保存和检索对象。它易于使用和处理。

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

从文件中检索数据

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

}