简体   繁体   English

如何从 ListView 和 SharedPreferences 中删除特定项目?

[英]How to remove a specific item from ListView and SharedPreferences?

I save History items (words) to SharedPreferences, and History is well loaded to view in a ListView.我将历史记录项(单词)保存到 SharedPreferences,并且历史记录被很好地加载以在 ListView 中查看。 I then use these code lines to delete a specific item from the ListView:然后我使用这些代码行从 ListView 中删除特定项目:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
        case R.id.edit:
            editNote(info.id);
            return true;
        case R.id.delete:
            String content = (String) mLSTHistory.getItemAtPosition(info.position);
               aptList.remove(content);
               aptList.notifyDataSetChanged();
            deleteNote(info.id);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}
private void deleteNote(long id) {
    // TODO Auto-generated method stub
    if (prefs.getBoolean("saveHistory", true) && mWordHistory != null && mWordHistory.size() >= 1)

    prefs =   PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    StringBuilder sbHistory = new StringBuilder();
    for (String item : mWordHistory)
    {
        sbHistory.append(item);
        sbHistory.append(",");

    String strHistory = sbHistory.substring(0, sbHistory.length()-1);
    SharedPreferences.Editor editor = prefs.edit();
    //editor.remove(content);
    editor.putString("history", strHistory);
    editor.commit();
    }
}
private void editNote(long id) {
    // TODO Auto-generated method stub

}

The selected item is definetely removed from the ListView, but it is not deleted from SharedPreferences in which it is saved.所选项目会从 ListView 中明确删除,但不会从保存它的 SharedPreferences 中删除。 Consequently, when the Listview is re-loaded, the item is still there.因此,当重新加载 Listview 时,该项目仍然存在。

My question is: how can I code to remove the selected item from SharePreferences as well?我的问题是:如何编写代码以从 SharePreferences 中删除所选项目? I mean how can I do to remove the selected item from both ListView and SharedPreferences?我的意思是如何从 ListView 和 SharedPreferences 中删除所选项目?

It would be great if you could provide instructions based on my code as I'm quite new with Android.如果您能根据我的代码提供说明,那就太好了,因为我对 Android 还是很陌生。 Thank you very much.非常感谢你。

To clear shared prefrences, Use要清除共享偏好,请使用

editor.clear();
editor.commit();

Also if, i have got your question correctly.另外,如果我正确地回答了您的问题。 To check if a value is stored in shared preferences, do this要检查值是否存储在共享首选项中,请执行以下操作

SharedPreferences mySharedPrefs = getSharedPreferences("MyPreferences", 0);
                if(mySharedPrefs.contains("theKey")) {
SharedPreferences.Editor editor = settings.edit();
                editor.remove("thekey");
                editor.commit();
}

try this试试这个

editor.remove("UserName");
editor.clear();
editor.putString("history",strHistory);   
editor.commit();

I have found the solution.我找到了解决办法。 Basically what you have to do is remove that particular item from your list view and then again add that updated list in sharedpreference so it will override the shared preference.基本上,您需要做的是从列表视图中删除该特定项目,然后再次在 sharedpreference 中添加该更新的列表,以便它将覆盖共享首选项。

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            addPlace.remove(i);
            
            arrayAdapter.notifyDataSetChanged();
            try {
                sharedPreferences.edit().putString("places", ObjectSerializer.serialize(MainActivity.addPlace)).apply();  add the updated shared pref again that will override previous saved.

            } catch (IOException e) {
                e.printStackTrace();
            }


            return false;
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM