Android中ListView实现常按删除

第一:当前类实现AdapterView.OnItemLongClickListener

第二:在findbyId下方设置mActivityCityDetailLv.setOnItemLongClickListener(this);

第三:实现未实现的方法:

@Override
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {

          AlertDialog.Builder builder=new AlertDialog.Builder(CityDetailActivity.this);
          builder.setMessage("确认删除?");
          builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
              @Override
      public void onClick(DialogInterface dialog, int which) {
          //删掉长按的item
          list.remove(position);
           //  动态更新listview
          adapter.notifyDataSetChanged();
      }
  });
  builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
          dialog.cancel();
      }
  });

    AlertDialog dialog=builder.create();
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();



    return false;
}

你可能感兴趣的:(Android中ListView实现常按删除)