Android通讯录开发之实现删除功能,rxjava原理面试

// 当用户打开菜单的时候触发

@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()) {

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

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 deleteContacts = new ArrayList();

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处理代码:

上一篇:通讯录Contact


下一篇:[html] 同一页面的跳转,锚文本的使用方法