android - ListView Removing Item is not working -


i have app wishlistactivity, reason, when click on remove button, removes 1 last on list.

this wishlistactivity:

public class wishlistactivity extends listactivity {  static arraylist<string> list; arrayadapter<string> arrayadapter;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      // creates new arraylists , populates     sharedpreferences prefs = wishlistactivity.this.getsharedpreferences("com.ben.sizeit", context.mode_private);      arraylist<string> samplelist = new arraylist<string>();     samplelist.add("sample item 1");     samplelist.add("sample item 2");     set<string> sampleset = new hashset<string>();     sampleset.addall(samplelist);      set<string> set = prefs.getstringset("wishlist", sampleset);     list = new arraylist<string>(set);      // create adapter passing arraylist 3rd parameter     arrayadapter = new wishlistadapter(this, list);      // sets adapter     setlistadapter(arrayadapter);      // allows user access home activity     getactionbar().setdisplayhomeasupenabled(true); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.wish_list, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     switch (item.getitemid()) {         case r.id.action_settings:             return true;         case android.r.id.home:             navutils.navigateupfromsametask(this);             return true;         case r.id.additem:             alertdialog.builder alert = new alertdialog.builder(this);              alert.settitle(r.string.newitem);             alert.setmessage(r.string.newitemtext);              // set edittext view user input             final edittext input = new edittext(this);             alert.setview(input);              alert.setpositivebutton(r.string.ok, new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int whichbutton) {                     string value = input.gettext().tostring();                     list.add(value);                     refresh();                      // saves list                     sharedpreferences prefs = wishlistactivity.this.getsharedpreferences("com.ben.sizeit", context.mode_private);                     set<string> set = new hashset<string>();                     set.addall(list);                     sharedpreferences.editor editor = prefs.edit();                     editor.putstringset("wishlist", set);                     editor.commit();                 }             });              alert.setnegativebutton(r.string.cancel, new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int whichbutton) {                     // canceled, nothing                 }             });              alert.show();              return true;         default:             return super.onoptionsitemselected(item);     } }  private void refresh() {     arrayadapter.notifydatasetchanged(); } 

}

this wishlistadapter:

public class wishlistadapter extends arrayadapter<string> {  private final arraylist<string> list; private final activity context; private viewholder viewholder;  public wishlistadapter(activity context, arraylist<string> list) {     super(context, r.layout.list_view_row_item, list);     this.context = context;     this.list = list; }  @override public view getview(int position, view convertview, viewgroup parent) {     view view = null;     if (convertview == null) {         layoutinflater inflator = context.getlayoutinflater();         view = inflator.inflate(r.layout.list_view_row_item, null);         viewholder = new viewholder();          viewholder.text = (textview) view.findviewbyid(r.id.itemlabel);         viewholder.button = (button) view.findviewbyid(r.id.removebutton);         viewholder.button.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 viewparent parentview = v.getparent();                 view parent = (view) parentview;                 log.e("debug", parent.tostring());                  textview itemlabeltextview = (textview) parent.findviewbyid(r.id.itemlabel);                  list.remove(list.indexof(itemlabeltextview.gettext().tostring()));                 wishlistadapter.this.notifydatasetchanged();                  // saves list                 sharedpreferences prefs = context.getsharedpreferences("com.ben.sizeit", context.mode_private);                 set<string> set = new hashset<string>();                 set.addall(list);                 sharedpreferences.editor editor = prefs.edit();                 editor.putstringset("wishlist", set);                 editor.commit();             }         });     } else {         view = convertview;     }      viewholder.text.settext(list.get(position));     return view; }  static class viewholder {     protected textview text;     protected button button; } 

}

i think wishlistadapter.this.notifydatasetchanged(); problem thing.
should using handler, wishlistactivity.java file in define handler.
if remove button click, send message whishlistactivity's handler.
handler call refresh().
, then, list refresh change data.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -