android - Delete a custom item when the button in it get clicked -
i have listview
custom list items having following layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/itemnumbertext" android:text="1." android:textsize="16sp" android:textstyle="bold" android:layout_height="match_parent" android:layout_width="33dp" android:gravity="center_vertical|center_horizontal"/> <imageview android:src="@drawable/item" android:id="@+id/imageview1" android:layout_height="match_parent" android:layout_width="47dp"/> <linearlayout android:id="@+id/linearlayout1" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="143dp"> <textview android:text="name" android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="16sp" android:textstyle="bold" android:textcolor="#f5cd10"></textview> <linearlayout android:id="@+id/linearlayout2" android:layout_height="wrap_content" android:layout_width="match_parent"> <textview android:id="@+id/text2" android:layout_height="wrap_content" android:text="amount" android:textcolor="#ffffff" android:layout_width="wrap_content"/> <textview android:text=" unit" android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <textview android:text="price" android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout> <relativelayout android:layout_height="match_parent" android:id="@+id/relativelayout1" android:layout_width="match_parent"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="itemdeletebuttonclicked" android:background="@drawable/item_button_style_selector" android:text="delete item" android:id="@+id/list_button" android:layout_alignparentright="true" android:layout_alignparentbottom="true"/> </relativelayout> </linearlayout>
as seen above mentioned method button, typing
android:onclick="itemdeletebuttonclicked"
here's code method:
public void itemdeletebuttonclicked(view v) { int index; index=itemslistview.getselecteditemposition();//itemslistview listview list.remove(index);//list list of data shown in listview adapter.notifydatasetchanged(); }
i have used simpleadapter
, it's not customized. here is:
//create base adapter listview adapter= new simpleadapter( this, list, r.layout.detailed_info_list_item, new string[] {"number","item","amount","price","unit"}, new int[] {r.id.itemnumbertext,r.id.text1,r.id.text2, r.id.text3,r.id.text4} ); setlistadapter(adapter);
but it's not working. it's not deleting clicked item. please give me suggestions.
the method getselecteditemposition()
returns right value if list item selected. if press button inside list item list item wont selected according list view. check value of index variable after have called getselecteditemposition()
. if -1 no list item selected , have selection of right list item yourself.
Comments
Post a Comment