scqyqly559
scqyqly559
采纳率100%
2015-04-24 05:37 阅读 4.9k

求指导!BaseAdapter监听的ListView如何设置长点击删除item

5

希望长点击一个item后会弹出AlertDialog,确定的话就会删除该ListView的item,
监听器是BaseAdapter

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    u011325156 aloneGirle 2015-04-24 06:07

    listview.setOnItemLongClickedListener(),方法里面可以使用removeItem(position);或者弹出Dialog,选择是否删除

    点赞 评论 复制链接分享
  • v587ge cc超人不会飞 2015-04-30 04:30
     public class MainActivity extends Activity {
    
        private List<String> myList;
        private ListView list;
        private MyAdapter adapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myList = new ArrayList<String>();
            list = (ListView) findViewById(R.id.list);
            myList.add("姓名1");
            myList.add("姓名2");
            myList.add("姓名3");
            myList.add("姓名4");
            myList.add("姓名5");
            adapter = new MyAdapter();
            list.setAdapter(adapter);
            list.setOnItemLongClickListener(new OnItemLongClickListener() {
    
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    myList.remove(position);
                    adapter.notifyDataSetChanged();
                    return false;
                }
            });
    
        }
        class MyAdapter extends BaseAdapter{
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return myList.size();
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return myList.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                ViewHolder viewHolder ;
                if(convertView == null){
                    convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_text,parent,false);
                    viewHolder = new ViewHolder();
                    viewHolder.myText = (TextView) convertView.findViewById(R.id.text);
                    convertView.setTag(viewHolder);
                }else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
    
                viewHolder.myText.setText(myList.get(position));
                return convertView;
            }
    
        }
    
        class ViewHolder{
            TextView myText ;
        }
    
    
    }
    
    

    你试试 XML也贴给你

    activity_main的 xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.work.list.MainActivity" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
    

    
    

    ListView内的
    <?xml version="1.0" encoding="utf-8"?>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" />
    

    
    
    点赞 3 评论 复制链接分享
  • bdmh bdmh 2015-04-24 05:50

相关推荐