I want remove checked elements from my list, but not only from main app but from SharedPreferences also. Now, in my app i can remove chcecked elements but not from SharedPreferences, so if i come back to my activity all of removed elements are still visible. Please, help me.
This is my 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();
}
}
}
To remove a specific saved pref then use
SharedPreferences.Editor editor = settings.edit();
editor.remove("tag_to_delete");
editor.commit();
To remove all of your saved preferences then use this
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
I found a problem. It's my mistake. I forgot clear a list and thats why the list duplicate itself. It's enough to add a localization.clear() method to solve this problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.