remove() or clear() in SharedPreferences does not work… here is a solution to the problem.
remove() or clear() in SharedPreferences does not work
I’m trying to clean up my Arraylist with SharedPreferences, but the code below doesn’t work:
public void btnLimparPref(View v){
SharedPreferences preferences = getSharedPreferences("listaPedidos", MODE_WORLD_WRITEABLE);
preferences.edit().clear().commit();
}
This code is
an activity, and the Arraylist is saved in a fragment, via this code:
void criaArray(String nomeProd, String descrProd) {
HashMap<String, String> map3 = new HashMap<String, String>();
map3.put(TAG_NM, nomeProd);
map3.put(TAG_DS, descrProd);
listaPedidos.add(map3);
getListView();
((BaseAdapter) adapter2).notifyDataSetChanged();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
Editor editor = prefs.edit();
try {
editor.putString("listaPedidos", ObjectSerializer.serialize(listaPedidos));
} catch (IOException e) {
e.printStackTrace();
}
editor.commit();
}
Any ideas? Thanks in advance!
Solution
You used two different SharedPreferences
objects in two code fragments.
First, you’re using:
getSharedPreferences("listaPedidos", MODE_WORLD_WRITEABLE)`
The second, which you are using:
PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
Changes you make to one do not affect the other.
Also, when you ask a question on Stack Overflow in the future, please provide a full explanation of what “not working” means.