【问题标题】:Delete ListView row Item with button click通过单击按钮删除 ListView 行项
【发布时间】:2015-02-03 05:53:37
【问题描述】:

我有一个简单的listview,每行都有一个删除按钮

现在,当我单击删除按钮时,应删除选定的列表视图行。

这是我的列表视图:

<?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" >

 <ListView android:id="@+id/itemslistview"
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:layout_marginTop="75dp"/>
</RelativeLayout>

ListViewRow:

<?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="match_parent">

    <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="2"
          android:padding="10dp"
          android:textSize="16sp"
          android:ems="10" 
          android:textColor="#800080" />
    <ImageView
           android:id="@+id/btndelete"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_weight="1" 
           android:src="@drawable/delete"
           android:layout_toRightOf="@+id/textView"
           android:layout_marginTop="5dp"/>
</LinearLayout>

这就是我尝试删除的方式:

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                                                   int position, long id) {
        listrowposition= position;
        Button del =(Button)findViewById(R.id.btndelete);
        findViewById(R.id.deleteicon).setOnClickListener(
                                        new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                MyList.remove(listrowposition);
                adp.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(),
                               "Deleted ListItem Number " + listrowposition,
                               Toast.LENGTH_LONG).show();
            }
        });
        Toast.makeText(getApplicationContext(),
                       "Click ListItem Number " + listrowposition,
                       Toast.LENGTH_LONG).show();

    }
});

【问题讨论】:

    标签: android listview selecteditem delete-row


    【解决方案1】:

    在你的代码中试试这个:

    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    listrowposition= position;
    Button del =(Button)view.findViewById(R.id.deleteicon);
    del.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                MyList.remove(listrowposition);
                adp.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(),
                          "Deleted ListItem Number " + listrowposition, Toast.LENGTH_LONG)
                          .show();
            }
        });
    
    
    Toast.makeText(getApplicationContext(), "Click ListItem Number " + listrowposition, Toast.LENGTH_LONG)
    .show();
    
    }
    });
    

    【讨论】:

      【解决方案2】:

      您需要在onClick 参数中使用View

      Button del =(Button) view.findViewById(R.id.btndelete);

      但请注意,findViewById 被认为不是性能明智的,因为它需要很长时间才能运行。最好使用holder pattern 并将onClick 设置在您的adapter 中。

      【讨论】:

      • 如果我使用这个应用程序会崩溃并且没有出现 logcat 错误
      • @coder 重启你的eclipse,它不可能没有错误地崩溃
      • 我想我无法获取按钮 ID,是否需要添加任何可聚焦来检测按钮?
      • @coder 我需要查看您的logcat 以确保...应该检测到id。您可以尝试在您的btndelete 中添加android:focusable=true
      【解决方案3】:

      试试这个方法,希望能帮助你解决问题。

      首先在 getView() 参数中将 position 定义为 final,然后尝试使用 从列表数据持有者 listDetail 中删除列表项remove 方法并使用 notifyDataSetChanged 方法通知您的适配器,例如:

      holder.removeButton.setOnClickListener(new OnClickListener()
      { 
          @Override
          public void onClick(View v)
          {
            listDetail.remove(position);
            notifyDataSetChanged();
            Log.i("Delete Button Clicked",*************************************************");
            Toast.makeText(context, "Delete button Clicked",Toast.LENGTH_LONG).show();
          }
      });
      

      【讨论】:

        【解决方案4】:

        首先要获取Listview和Button的点击事件,需要将Button的Focusable()和FocusableInTouchMode()属性设置为FALSE。

        现在,在您的自定义适配器的 getView() 方法中,您可以在列表中获取对您的 Button 的引用。在您的按钮的那个 setTag(position) 中。

        给这个Button设置onClickListener,当你得到onClick(View view)回调的时候可以调用view的getTag()方法(就是list item的位置)。

        最后,使用该位置,您可以从列表中删除元素并调用适配器的 notifyDataSetChanged()。

        【讨论】:

          【解决方案5】:

          用简单的动画删除。

          listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          
                @Override
                public void onItemClick(AdapterView<?> parent, final View view,
                    int position, long id) {
                  final String item = (String) parent.getItemAtPosition(position);
                  view.animate().setDuration(2000).alpha(0)
                      .withEndAction(new Runnable() {
                        @Override
                        public void run() {
                          list.remove(item);
                          adapter.notifyDataSetChanged();
                          view.setAlpha(1);
                        }
                      });
                }
          
              });
            }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-04-29
            • 2013-10-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-15
            相关资源
            最近更新 更多