Remove () or clear () from SharedPreferences doesn't work
I am trying to clear my Arraylist using SharedPreferences, but the code below does not work:
public void btnLimparPref(View v){
SharedPreferences preferences = getSharedPreferences("listaPedidos", MODE_WORLD_WRITEABLE);
preferences.edit().clear().commit();
}
This code represents an Activity, and the Arraylist is saved in a fragment by 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!
+3
source to share
1 answer
You are using two different objects SharedPreferences
in two pieces of code.
In the first case, you use:
getSharedPreferences("listaPedidos", MODE_WORLD_WRITEABLE)`
In the second, you use:
PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext())
Changes you make to one of them do not affect the others.
Also, in the future, when you ask questions about Stack Overflow, please provide a full explanation of what "not working" means.
+4
source to share