android功能模块之通讯功能模块一

熊孩纸 阅读:642 2021-04-01 00:24:55 评论:0



2014年1月15日

无论是Android开发或者是其他移动平台的开发,ListView肯定是一个大咖,那么对ListView的操作肯定是不会少的,上一篇博客介绍了如何实现全选和反选的功能,本篇博客介绍删除功能,删除列表中的项无谓就是及时刷新列表,这又跟UI线程扯上关系了,还是那句话,数据的更新通知一定要在UI线程上做,不然会出现各种错误,比如出现adapter数据源改变,但没有及时收到通知的情况。在执行遍历删除的时候,最好不要每删一个就直接通知,下面是我的实现方法,将需要删除的contact保存到一个List然后通过handler发生消息,然后再由handler来处理。




这里我顺带提一下菜单的实现

1、定义menu下的xml文件

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <group android:id="@+id/metting_menu" >  
  4.         <item  
  5.             android:id="@+id/select_all_item"  
  6.             android:icon="@drawable/select_all_not"  
  7.             android:title="全选">  
  8.         </item>  
  9.         <item  
  10.             android:id="@+id/delete_item"  
  11.             android:icon="@drawable/delete"  
  12.             android:title="删除">  
  13.         </item>  
  14.         <item  
  15.             android:id="@+id/sync_item"  
  16.             android:icon="@drawable/sync_manage"  
  17.             android:title="同步">  
  18.         </item>  
  19.     </group>  
  20.   
  21. </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. 代码实现

  1. /** 
  2.      * 创建菜单 
  3.      */  
  4.     @Override  
  5.     public boolean onCreateOptionsMenu(Menu menu) {  
  6.         MenuInflater inflater = getMenuInflater();  
  7.         inflater.inflate(R.menu.meetting_contact_menu, menu);  
  8.         selectAllItem = menu.findItem(R.id.select_all_item);  
  9.         return true;  
  10.     }  
  11.   
  12.     // 当用户打开菜单的时候触发  
  13.     @Override  
  14.     public boolean onMenuOpened(int featureId, Menu menu) {  
  15.         if (menu != null) {  
  16.             refreshSelectAllItemIcon();  
  17.         }  
  18.         return super.onMenuOpened(featureId, menu);  
  19.     }  
  20.   
  21.     /** 
  22.      * menu的点击事件 
  23.      */  
  24.     @Override  
  25.     public boolean onOptionsItemSelected(MenuItem item) {  
  26.         switch (item.getItemId()) {  
  27.         case R.id.select_all_item:  
  28.             if (isAllChecked) { // 反选  
  29.                 item.setIcon(R.drawable.select_all_not);  
  30.                 invertSelection();  
  31.             } else { // 全选  
  32.                 if (contactList.size() == 0) {  
  33.                     Toast.makeText(this"目前没有可选的联系人", Toast.LENGTH_SHORT)  
  34.                             .show();  
  35.                     break;  
  36.                 }  
  37.                 item.setIcon(R.drawable.select_all);  
  38.                 selectAllContacts();  
  39.             }  
  40.             break;  
  41.         case R.id.delete_item: // 删除  
  42.             if (count == 0) {  
  43.                 Toast.makeText(this"您还没有选择任何联系人", Toast.LENGTH_SHORT).show();  
  44.                 break;  
  45.             }  
  46.             showDeleteDialog(this"您确定要删除吗");  
  47.             isAllChecked = false;  
  48.             selectAllItem.setIcon(R.drawable.select_all_not);  
  49.             break;  
  50.         case R.id.sync_item:  
  51.             Intent fresh_intent = new Intent(this, CloudSyncActivity.class);  
  52.             if (flag) { // 如果是会议  
  53.                 fresh_intent.putExtra("number", meetingNumber);  
  54.             }  
  55.             startActivityForResult(fresh_intent, CLOUD_SYNC);  
  56.             break;  
  57.         default:  
  58.             break;  
  59.         }  
  60.         return false;  
  61.     }  
  62.   
  63.     // 刷新菜单项全选  
  64.     private void refreshSelectAllItemIcon() {  
  65.         for (String contactId : map_NumberSelected.keySet()) {  
  66.             if (!map_NumberSelected.get(contactId)) {  
  67.                 isAllChecked = false;  
  68.                 selectAllItem.setIcon(R.drawable.select_all_not);  
  69.                 break;  
  70.             }  
  71.             isAllChecked = true;  
  72.             selectAllItem.setIcon(R.drawable.select_all);  
  73.         }  
  74.     }  
/** 
	 * 创建菜单 
	 */ 
	@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); 
		} 
	}


提示删除的对话框

  1. /** 显示确认删除对话框 **/  
  2.     public void showDeleteDialog(Context context, String msg) {  
  3.         final Dialog dialog = new Dialog(context, R.style.MyDialog);  
  4.         View view = LayoutInflater.from(context).inflate(R.layout.back_dialog,  
  5.                 null);  
  6.         TextView msg_tv = (TextView) view.findViewById(R.id.msg);  
  7.         msg_tv.setText(msg);  
  8.         Button sure = (Button) view.findViewById(R.id.sure);  
  9.         Button cancle = (Button) view.findViewById(R.id.cancle);  
  10.         dialog.setContentView(view);  
  11.         sure.setOnClickListener(new OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.                 deleteContacts();  
  15.                 dialog.dismiss();  
  16.             }  
  17.         });  
  18.         cancle.setOnClickListener(new OnClickListener() {  
  19.   
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 dialog.dismiss();  
  23.             }  
  24.         });  
  25.         dialog.show();  
  26.     }  
/** 显示确认删除对话框 **/ 
	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(); 
	} 




下面是删除操作:

这里只关注实现删除的脉路,其中有些代码是业务逻辑需要可以稍微看一下就可以了

  1. // 删除选中的联系人  
  2. public void deleteContacts() {  
  3.     DialogHelp.getInstance().showHttpDialog(this, R.string.wait_for_while,  
  4.             getString(R.string.deleting_contact));  
  5.     new Thread() {  
  6.         @Override  
  7.         public void run() {  
  8.             ArrayList<Contact> deleteContacts = new ArrayList<Contact>();  
  9.             for (Contact contact : contactList) {  
  10.                 boolean isselected = map_NumberSelected  
  11.                         .get(contact.getId());  
  12.                 if (isselected) {  
  13.                     for (RawContact rawContact : rawList) {  
  14.                         if (contact.getUserId() == rawContact.getUserId()) { // 是同一个id  
  15.                             if (contact.getNumber() == rawContact  
  16.                                     .getPhone1()) {  
  17.                                 rawContact.setPhone1(null);  
  18.                             } else if (contact.getNumber() == rawContact  
  19.                                     .getPhone2()) {  
  20.                                 rawContact.setPhone2(null);  
  21.                             } else if (contact.getNumber() == rawContact  
  22.                                     .getTelephone()) {  
  23.                                 rawContact.setTelephone(null);  
  24.                             }  
  25.                             if ((rawContact.getPhone1() == null || ""  
  26.                                     .equals(rawContact.getPhone1()))  
  27.                                     && (rawContact.getPhone2() == null || ""  
  28.                                             .equals(rawContact.getPhone2()))  
  29.                                     && (rawContact.getTelephone() == null || ""  
  30.                                             .equals(rawContact  
  31.                                                     .getTelephone()))) {  
  32.                                 MyDataBase.deleteContact(  
  33.                                         MeetingContactActivity.this,  
  34.                                         rawContact);  
  35.                             } else {  
  36.                                 MyDataBase.updateContactById(  
  37.                                         MeetingContactActivity.this,  
  38.                                         rawContact);  
  39.                             }  
  40.                             break;  
  41.                         }  
  42.                     }  
  43.                     // 将需要删除的联系人保存下来  
  44.                     deleteContacts.add(contact);  
  45.                 }  
  46.             }  
  47.             Message msg = new Message();  
  48.             msg.obj = deleteContacts;  
  49.             msg.what = DELETE_COMPLETE;  
  50.             handler.sendMessage(msg);  
  51.         };  
  52.     }.start();  
  53.   
  54. }  
	// 删除选中的联系人 
	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的分支进行通知列表刷新。

  1. private Handler handler = new Handler() {  
  2.         @Override  
  3.         public void handleMessage(Message msg) {  
  4.             if (msg.obj != null) {  
  5.                 ArrayList<Contact> deleteContacts = (ArrayList<Contact>) msg.obj;  
  6.                 for (Contact contact : deleteContacts) {  
  7.                     map_NumberSelected.remove(contact.getId());  
  8.                     contactList.remove(contact);  
  9.                 }  
  10.             }  
  11.             switch (msg.what) {  
  12.             case LOAD_CONTACT_FINISHED:  
  13.                 // 判断是否为空  
  14.                 if (contactList == null || contactList.size() == 0) {  
  15.                     syncLayout.setVisibility(View.VISIBLE);  
  16.                     bottomLayout.setVisibility(View.GONE);  
  17.                     listView.setVisibility(View.GONE);  
  18.                 } else {  
  19.                     syncLayout.setVisibility(View.GONE);  
  20.                     bottomLayout.setVisibility(View.VISIBLE);  
  21.                     listView.setVisibility(View.VISIBLE);  
  22.                 }  
  23.                 adapter.setContactList(contactList);  
  24.                 refreshList();  
  25.                 DialogHelp.getInstance().dismissDialog();  
  26.                 break;  
  27.             case CLOUD_SYNC: // 同步  
  28.                 initContacts();  
  29.                 refreshList();  
  30.                 break;  
  31.             case SEARCH_FINISH:  
  32.                 refreshList();  
  33.                 break;  
  34.             case DELETE_COMPLETE: // 删除完毕  
  35.                 DialogHelp.getInstance().dismissDialog();  
  36.                 // 重新初始化  
  37.                 refreshList();  
  38.                 break;  
  39.             }  
  40.         }  
  41.     };  
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; 
			} 
		} 
	}; 

以上就是删除功能的全部实现了,关于通讯录模块开发博客可能会暂停更新,比如想字母索引,联系人分组等功能我还没去实现,以后有机会会继续分享。


标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号