单击自定义项目中的按钮时删除该项目

发布于 2024-12-08 18:18:54 字数 4011 浏览 0 评论 0原文

我有一个 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);

但是它不起作用。也就是说,它不会删除单击的项目。请给我一些建议好吗?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她如夕阳 2024-12-15 18:18:54

仅当确实选择了列表项时,getSelectedItemPosition() 方法才会返回正确的值。如果您按下列表项内的按钮,则可能不会根据列表视图选择列表项。调用 getSelectedItemPosition() 后检查索引变量的值。如果为 -1,则不会选择任何列表项,您必须自己选择正确的列表项。

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.

寻找一个思念的角度 2024-12-15 18:18:54

在列表视图上设置一个 OnItemClickListener 将项目位置存储到字段中,然后您就会知道选择了哪个项目。

您可以尝试调用 setSelection 来使 getSelectedItemPosition 工作,但设置字段更简单

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文