![](/img/trans.png)
[英]Android - Delete an item from Custom Listview and update it when long clicked
[英]Delete a custom item when the button in it get clicked
我有一个带有自定义列表项的ListView
,其布局如下:
<?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>
从上面可以看到,我提到了Button的方法,键入
android:onClick="itemDeleteButtonClicked"
这是方法的代码:
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();
}
我已经使用了SimpleAdapter
并且它不是自定义的。 这里是:
//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);
但这不起作用。 那是不是在删除被点击的项目。 请给我一些建议。
如果确实选择了列表项,则方法getSelectedItemPosition()
仅返回正确的值。 如果您在列表项中按下按钮,则可能不会根据列表视图选择该列表项。 调用getSelectedItemPosition()
之后,检查索引Variable的值。 如果为-1,则未选择任何列表项,您必须自己选择正确的列表项。
在列表视图上设置一个OnItemClickListener
,以将项目位置存储到字段中,然后您将知道选择了哪个项目。
您可以尝试调用setSelection使getSelectedItemPosition工作,但设置字段更简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.