android - How to remove a custom view from list view using a button in the custom view -


i created custom view edit text field , button. want delete item listview clicking on button. code custom view:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent" android:layout_height="match_parent">      <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="delete"         android:id="@+id/delete_btn"         android:layout_alignparenttop="true"         android:layout_alignparentright="true"         android:layout_alignparentend="true" />      <edittext         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:id="@+id/player_name_txt"         android:textcolor="#ffffff"         android:editable="false"         android:background="#5eb8ed"         android:layout_alignparenttop="true"         android:layout_alignparentleft="true"         android:layout_alignparentstart="true"         android:layout_alignbottom="@+id/delete_btn"         android:layout_toleftof="@+id/delete_btn"         android:layout_tostartof="@+id/delete_btn" />     </relativelayout> 

the code adapter , list view:

inflater = (layoutinflater)this.getsystemservice(context.layout_inflater_service);     playerlistview = (listview)this.findviewbyid(android.r.id.content).getrootview().findviewbyid(r.id.player_list);     playerlistadapter = new arrayadapter(this,r.layout.player_item,r.id.player_name_txt, new arraylist<string>(){}); playerlistview.setadapter(playerlistadapter); 

the code tried:

 public void removeplayer(view v)     {         edittext pairededit = (edittext)findviewbyid(r.id.player_name_txt);         string name = pairededit.gettext().tostring();         playerlistadapter.remove(name);         playerlistadapter.notifydatasetchanged();     } 

right removes first item in list view.

alertdialog.builder addplayerbuilder = new alertdialog.builder(this);         final view customview = inflater.inflate(r.layout.add_player,null);         final edittext usernameedit = (edittext)customview.findviewbyid(r.id.username_edit);         addplayerbuilder.setview(customview);         addplayerbuilder.setpositivebutton("ok", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int id) {                  string name = usernameedit.gettext().tostring();                 playerlistadapter.add(name);                 playerlistadapter.notifydatasetchanged();                  dialog.dismiss();              }         });         addplayerbuilder.setnegativebutton("cancel", new dialoginterface.onclicklistener() {             @override             public void onclick(dialoginterface dialog, int id) {                 dialog.cancel();             }         });          alertdialog dialog = addplayerbuilder.create();         dialog.show(); 

in button of custom layout place onclicklistener remove element arraylist remove() function on basis of position , call notifydatasetinvalidated()


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -