长按条目进行删除
2016-06-30 16:07
218 查看
==================方法一=====================
//长按进行删除
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle(lp.get(position).getName());
String name[] = {"删除","数据共享","备份"};
builder.setSingleChoiceItems(name, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
ConDao cd = new ConDao(MainActivity.this);
cd.del(lp.get(position).getId());
lp = cd.sel();
adapter.refresh(lp);
//关闭
d.dismiss();
break;
default:
break;
}
}
});
d = builder.show();
return false;
}
});
============================方法二 ==================
==在MainActivity中的oncreaView中==
registerForContextMenu(lv);
=====oncreaView中下面====
重写这2个方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("");
menu.add(0, 1, 1, "删除");
menu.add(0, 2, 2, "共享");
menu.add(0, 3, 3, "复制联系人");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
int position = menuInfo.position;
ConDao cdo = new ConDao(MainActivity.this);
cd.del(lp.get(position).getId());
lp = cd.sel();
adapter.refresh(lp);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
//长按进行删除
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle(lp.get(position).getName());
String name[] = {"删除","数据共享","备份"};
builder.setSingleChoiceItems(name, 0, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
ConDao cd = new ConDao(MainActivity.this);
cd.del(lp.get(position).getId());
lp = cd.sel();
adapter.refresh(lp);
//关闭
d.dismiss();
break;
default:
break;
}
}
});
d = builder.show();
return false;
}
});
============================方法二 ==================
==在MainActivity中的oncreaView中==
registerForContextMenu(lv);
=====oncreaView中下面====
重写这2个方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderTitle("");
menu.add(0, 1, 1, "删除");
menu.add(0, 2, 2, "共享");
menu.add(0, 3, 3, "复制联系人");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
int position = menuInfo.position;
ConDao cdo = new ConDao(MainActivity.this);
cd.del(lp.get(position).getId());
lp = cd.sel();
adapter.refresh(lp);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
相关文章推荐
- 一个可以用鼠标改变控件位置和大小的类
- AndroidSweetSheet:ViewPager的实现(2)
- [leetcode]Longest Palindromic Substring
- “Unable to open kernel device \\.\Global\vmx86
- 微信浏览器调试
- ListVIew简单实现多布局
- android 百度地图系列之地图初始化及定位
- android屏幕适配
- BAPI_GOODSMVT_CREATE for subcontracting PO
- centos启用ftp功能
- python下的socket常用方法举例
- python下的socket常用方法举例
- Linux文本工具sed
- 关于ajax的页面加载操作防止脏数据的产生
- JAVA自学之每日一题(三)
- ScyllaDB 1.2 国内安装更新源发布
- 短信管理器(SmsManager)
- Struts2框架学习(三) 数据处理
- 解决ListView中Item的子控件(比如Button)与Item点击事件冲突
- java.lang.IllegalArgumentException: Page directive: invalid value for import