开发者

Delete a custom item when the button in it get clicked

I have a ListView with 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 its seen from above I mentioned a method for Button, typing

android:onClick="itemDeleteButtonClicked" 

Here's the code for method:

 public void itemDeleteButtonClicked(View v)
 {
    int index;
    index=itemsListView.getSelectedItemPosition();//itemsListView is the listview
    list.remove(index);//list is the list of data to be shown in listview
    adapter.notifyDataSetChanged();
 }

I have used a SimpleAdapter and it's not customized. Here it is:

//create base adapter for 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. That is it's not deleting the clicked item. Would you please give me some suggestions.


The method getSelectedItemPosition() only returns the right value if the list item is really selected. If you press a Button inside the list item the list item probably wont be selected according to the list view. Check the value of the index Variable after you have called getSelectedItemPosition(). If it is -1 no list item is selected and you have to do the selection of the right list item yourself.


Set an OnItemClickListener on your listview to store the item position into a field then you will know which item was selected.

You could try calling setSelection to make getSelectedItemPosition work but setting a field is simpler

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜