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

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

3个回答

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

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


Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问