gpt4 book ai didi

java - 从 SharedPreference 列表中删除项目 [android]

转载 作者:行者123 更新时间:2023-11-29 09:43:43 25 4
gpt4 key购买 nike

我想从我的列表中删除选中的元素,但不仅要从主应用程序中删除,还要从 SharedPreferences 中删除。现在,在我的应用程序中,我可以删除 chcecked 元素,但不能从 SharedPreferences 中删除,所以如果我回到我的 Activity ,所有删除的元素仍然可见。请帮帮我。

这是我的 Activity :

    SharedPreferences preferences;
ArrayList<Object> list = new ArrayList<Object>();
ArrayAdapter<Object> adapter;
List<String> localization;
Button btnDelete;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_moje_miejsca);

btnDelete = (Button) findViewById(R.id.btnDelete);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, list);
preferences = getSharedPreferences("coordinates", Activity.MODE_PRIVATE);

Set<String> localizationSet = preferences.getStringSet("localization_set", new HashSet<String>());
localization = new ArrayList<>(localizationSet);

for (String listPosition : localizationSet) {
list.add(listPosition);
adapter.notifyDataSetChanged();
}

setListAdapter(adapter);


public void onClickBtnDelete(View view){

SharedPreferences.Editor editor = preferences.edit();
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int itemCount = getListView().getCount();

for(int i = itemCount-1; i >= 0; i--){

if(checkedItemPositions.get(i)){
int position = i + 1;
adapter.remove(list.get(i));

}
}

editor.remove("localization_set").commit();
itemCount = getListView().getCount();

checkedItemPositions.clear();
adapter.notifyDataSetChanged();

for (int i = itemCount-1; i >= 0; i--){

localization.add((String) list.get(i));
setLocalization = new HashSet<String>(localization);
editor.putStringSet("localization_set", setLocalization).commit();

}

}

}

最佳答案

要删除特定的保存首选项然后使用

SharedPreferences.Editor editor = settings.edit();
editor.remove("tag_to_delete");
editor.commit();

要删除所有已保存的首选项,请使用此

SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();

关于java - 从 SharedPreference 列表中删除项目 [android],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24131732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com