android - How do you delete a sharedPreference by clicking on a listView item? -
i have created list displays shared preference string. create this...
adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listitems); setlistadapter(adapter); if (sharedpreferences.contains(name0)) { string prefs = sharedpreferences.getstring(name0, ""); listitems.add(prefs); }
and on other preferences. trying figure out how delete list item, , shared preference along well. can't figure out if should use onclick listener, or delete button in list or other method perhaps haven't read about. i'd make simple , intuitive user.
so question is, how can edit existing code delete list item , preference displaying. thank help.
this ended figuring out on weekend, takes shared preference has 2 values comma separated. ex. (wifiname_ssid,john's wifi). splits shared preference values, , checks see if shared preference values match string stored in list. if shared preference , list item clicked match, item deleted.
adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, listitems); lv = (listview) findviewbyid(android.r.id.list); lv.setadapter(adapter); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> a, view v, int position, long id) { alertdialog.builder adb = new alertdialog.builder( nicknameprefs.this); adb.settitle("delete?"); adb.setmessage("are sure want delete \n" + adapter.getitem(position)); final int positiontoremove = position; adb.setnegativebutton("cancel", null); adb.setpositivebutton("ok", new alertdialog.onclicklistener() { public void onclick(dialoginterface dialog, int which) { string item = adapter.getitem(positiontoremove); list<string> items = arrays.aslist(item .split("\\s*,\\s*")); adapter.remove(adapter.getitem(positiontoremove)); string prefs1 = sharedpreferences.getstring(name0, ""); list<string> items1 = arrays.aslist(prefs1 .split("\\s*,\\s*")); if (item.contains(items1.get(0)) || item.contains(items1.get(1))) { sharedpreferences.edit().remove(name0).apply(); } adapter.notifydatasetchanged(); } }); adb.show(); } });
Comments
Post a Comment