无论是android开发或者是其他移动平台的开发,listview肯定是一个大咖,那么对listview的操作肯定是不会少的,上一篇博客介绍了如何实现全选和反选的功能,本篇博客介绍删除功能,删除列表中的项无谓就是及时刷新列表,这又跟ui线程扯上关系了,还是那句话,数据的更新通知一定要在ui线程上做,不然会出现各种错误,比如出现adapter数据源改变,但没有及时收到通知的情况。在执行遍历删除的时候,最好不要每删一个就直接通知,下面是我的实现方法,将需要删除的contact保存到一个list然后通过handler发生消息,然后再由handler来处理。
这里我顺带提一下菜单的实现
1、定义menu下的xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group android:id="@+id/metting_menu" >
<item
android:id="@+id/select_all_item"
android:icon="@drawable/select_all_not"
android:title="全选">
</item>
<item
android:id="@+id/delete_item"
android:icon="@drawable/delete"
android:title="删除">
</item>
<item
android:id="@+id/sync_item"
android:icon="@drawable/sync_manage"
android:title="同步">
</item>
</group>
</menu>
icon为菜单图标
title为菜单文本
2. 代码实现
/**
* 创建菜单
*/
@override
public boolean oncreateoptionsmenu(menu menu) {
menuinflater inflater = getmenuinflater();
inflater.inflate(r.menu.meetting_contact_menu, menu);
selectallitem = menu.finditem(r.id.select_all_item);
return true;
}
// 当用户打开菜单的时候触发
@override
public boolean onmenuopened(int featureid, menu menu) {
if (menu != null) {
refreshselectallitemicon();
}
return super.onmenuopened(featureid, menu);
}
/**
* menu的点击事件
*/
@override
public boolean onoptionsitemselected(menuitem item) {
switch (item.getitemid()) {
case r.id.select_all_item:
if (isallchecked) { // 反选
item.seticon(r.drawable.select_all_not);
invertselection();
} else { // 全选
if (contactlist.size() == 0) {
toast.maketext(this, "目前没有可选的联系人", toast.length_short)
.show();
break;
}
item.seticon(r.drawable.select_all);
selectallcontacts();
}
break;
case r.id.delete_item: // 删除
if (count == 0) {
toast.maketext(this, "您还没有选择任何联系人", toast.length_short).show();
break;
}
showdeletedialog(this, "您确定要删除吗");
isallchecked = false;
selectallitem.seticon(r.drawable.select_all_not);
break;
case r.id.sync_item:
intent fresh_intent = new intent(this, cloudsyncactivity.class);
if (flag) {// 如果是会议
fresh_intent.putextra("number", meetingnumber);
}
startactivityforresult(fresh_intent, cloud_sync);
break;
default:
break;
}
return false;
}
// 刷新菜单项全选
private void refreshselectallitemicon() {
for (string contactid : map_numberselected.keyset()) {
if (!map_numberselected.get(contactid)) {
isallchecked = false;
selectallitem.seticon(r.drawable.select_all_not);
break;
}
isallchecked = true;
selectallitem.seticon(r.drawable.select_all);
}
}
提示删除的对话框
/** 显示确认删除对话框 **/
public void showdeletedialog(context context, string msg) {
final dialog dialog = new dialog(context, r.style.mydialog);
view view = layoutinflater.from(context).inflate(r.layout.back_dialog,
null);
textview msg_tv = (textview) view.findviewbyid(r.id.msg);
msg_tv.settext(msg);
button sure = (button) view.findviewbyid(r.id.sure);
button cancle = (button) view.findviewbyid(r.id.cancle);
dialog.setcontentview(view);
sure.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
deletecontacts();
dialog.dismiss();
}
});
cancle.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
dialog.dismiss();
}
});
dialog.show();
}
下面是删除操作:
这里只关注实现删除的脉路,其中有些代码是业务逻辑需要可以稍微看一下就可以了
// 删除选中的联系人
public void deletecontacts() {
dialoghelp.getinstance().showhttpdialog(this, r.string.wait_for_while,
getstring(r.string.deleting_contact));
new thread() {
@override
public void run() {
arraylist<contact> deletecontacts = new arraylist<contact>();
for (contact contact : contactlist) {
boolean isselected = map_numberselected
.get(contact.getid());
if (isselected) {
for (rawcontact rawcontact : rawlist) {
if (contact.getuserid() == rawcontact.getuserid()) { // 是同一个id
if (contact.getnumber() == rawcontact
.getphone1()) {
rawcontact.setphone1(null);
} else if (contact.getnumber() == rawcontact
.getphone2()) {
rawcontact.setphone2(null);
} else if (contact.getnumber() == rawcontact
.gettelephone()) {
rawcontact.settelephone(null);
}
if ((rawcontact.getphone1() == null || ""
.equals(rawcontact.getphone1()))
&& (rawcontact.getphone2() == null || ""
.equals(rawcontact.getphone2()))
&& (rawcontact.gettelephone() == null || ""
.equals(rawcontact
.gettelephone()))) {
mydatabase.deletecontact(
meetingcontactactivity.this,
rawcontact);
} else {
mydatabase.updatecontactbyid(
meetingcontactactivity.this,
rawcontact);
}
break;
}
}
// 将需要删除的联系人保存下来
deletecontacts.add(contact);
}
}
message msg = new message();
msg.obj = deletecontacts;
msg.what = delete_complete;
handler.sendmessage(msg);
};
}.start();
}
deletecontacts是用来保存需要删除的联系人,把它赋给msg.obj,再通过handler将消息放入消息队列,最终在handler的handlemessage方法进行删除操作。
handler处理代码:
这里把传递过来list在contactlist遍历删除,然后在msg.what为delete_complete的分支进行通知列表刷新。
private handler handler = new handler() {
@override
public void handlemessage(message msg) {
if (msg.obj != null) {
arraylist<contact> deletecontacts = (arraylist<contact>) msg.obj;
for (contact contact : deletecontacts) {
map_numberselected.remove(contact.getid());
contactlist.remove(contact);
}
}
switch (msg.what) {
case load_contact_finished:
// 判断是否为空
if (contactlist == null || contactlist.size() == 0) {
synclayout.setvisibility(view.visible);
bottomlayout.setvisibility(view.gone);
listview.setvisibility(view.gone);
} else {
synclayout.setvisibility(view.gone);
bottomlayout.setvisibility(view.visible);
listview.setvisibility(view.visible);
}
adapter.setcontactlist(contactlist);
refreshlist();
dialoghelp.getinstance().dismissdialog();
break;
case cloud_sync: // 同步
initcontacts();
refreshlist();
break;
case search_finish:
refreshlist();
break;
case delete_complete: // 删除完毕
dialoghelp.getinstance().dismissdialog();
// 重新初始化
refreshlist();
break;
}
}
};
以上就是删除功能的全部实现了,关于通讯录模块开发博客可能会暂停更新,比如想字母索引,联系人分组等功能我还没去实现,以后有机会会继续分享。