android功能模块之通讯功能模块一
2014年1月15日
无论是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>
<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);
- }
- }
/**
* 创建菜单
*/
@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 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();
- }
// 删除选中的联系人
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;
- }
- }
- };
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;
}
}
};
以上就是删除功能的全部实现了,关于通讯录模块开发博客可能会暂停更新,比如想字母索引,联系人分组等功能我还没去实现,以后有机会会继续分享。
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。