web技术

联系我们

web技术位置:主页 > web技术 >

如何获取android手机联系人并按字母展示(一)

编辑:AG集&# 时间:2019-02-01

如何获取android手机联系人并按字母展示(一)

android提供了本地数据库的查询uri,可以查询出数据:

采用一个AsyncQueryHandler来进行查询, AsyncQueryHandler自己开启了线程来进行数据查询,很方便


protected AsyncQueryHandler mQueryHandler;

protected final void queryPersonal() {
		mQueryHandler.startQuery(QUERY_TOKEN, null, ContactsContract.Contacts.CONTENT_URI, 
				Personal.CONTACTS_SUMMARY_PROJECTION, null, null, getSortOrder(ContactsContract.Contacts.DISPLAY_NAME));
	}

	
	protected static String getSortOrder(String fieldName) {
		//substr为截取函数,取第一个字母
		//COLLATE主要用于对字符进行排
		//COLLATE LOCALIZED 按本地语言进行排序
		return "CASE WHEN substr(UPPER(" + fieldName + "), 1, 1) BETWEEN 'A' AND 'Z' THEN 1 else 10 END," +
				fieldName + " COLLATE LOCALIZED ASC";
	}

protected final class MyHandler extends AsyncQueryHandler { 
		/**
		 * Asynchronous query handler constructor.
		 */
		public MyHandler(Context context) {
			super(context.getContentResolver());
		}
		
		/**
		 * On query completion.
		 */
		@Override
		protected void onQueryComplete(int token, Object cookie, Cursor cursor) { //handler查询完的回调
			if (cursor == null || cursor.isClosed()) {
				return;
			}
			if (!isFinishing()) {
				setLoading(false);
				if (mAdapter != null) {
					mAdapter.setLoading(false);
					mAdapter.changeCursor(cursor);
				} 
				
				if (cursor.getCount() == 0) {
					mEmtytext.setVisibility(View.VISIBLE);
				} else {
					mEmtytext.setVisibility(View.INVISIBLE);
				}
			} else {
				if (cursor != null && !cursor.isClosed()) {
					cursor.close();
				}
			}
		}
	}

这个Layout是:





    

    

        

        
    



然后是list adapter的写法:

	protected static final class SectionedContactListItemCache {
		public TextView sectionHeader;
		public TextView nameView;
		public TextView typeView;
		public ImageView photoView;
		public ImageView detailItemIcon;
        	public View nameTypeWrapper;
	}
	
	
	protected final class ContactsAdapter extends ResourceCursorAdapter  {


		
		public ContactsAdapter(Context context) {
			super(context, R.layout.contacts_list_item_photo,null);
		}

		
		@Override
		public void changeCursor(Cursor c) {
			super.changeCursor(c);
		}
		
		protected String getTitle(String displayName) {
			String title;
			/** check if the first letter is English letter */
			Matcher matcher = mPattern.matcher(displayName);
			if (!matcher.find()) {
				title = NONE_ENGLISH_LETTER_TITLE;
			} else {
				title = displayName.trim().substring(0, 1).toUpperCase(Locale.US);
			}
			return title;
		}
		
		protected String getDisplayName(Cursor c) {
			
			String displayName =  c.getString(Personal.NAME_COLUMN_INDEX);
					
			if(TextUtils.isEmpty(displayName)) {
				return "";
			}
			
			return displayName;
		}
		


		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			final SectionedContactListItemCache cache = (SectionedContactListItemCache) view.getTag();
			cache.typeView.setVisibility(View.GONE);
			cache.photoView.setVisibility(View.VISIBLE);
			String name = cursor.getString(Personal.NAME_COLUMN_INDEX);
			if (TextUtils.isEmpty(name)) {
				cache.nameView.setText(R.string.contact_no_name);
			} else {
				cache.nameView.setText(name);
			}
		}
		
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			
			View view = super.newView(context, cursor, parent);
			final SectionedContactListItemCache cache = new SectionedContactListItemCache();
            cache.nameTypeWrapper = view.findViewById(R.id.name_type);
			cache.sectionHeader = (TextView) view.findViewById(R.id.txtSectionHeader);
			cache.nameView = (TextView) view.findViewById(R.id.name);
			cache.typeView = (TextView) view.findViewById(R.id.type);
			cache.photoView = (ImageView) view.findViewById(R.id.photo);
			cache.detailItemIcon = (ImageView) view.findViewById(R.id.contacts_detail_item_icon);
			view.setTag(cache);
			
			return view;
		}
		
		
	}//end of adapter

item adapter的layout:




    

    

        

        

        

        

            

            
        
    



http://www.bkjia.com/Androidjc/866726.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/866726.htmlTechArticle如何获取android手机联系人并按字母展示(一) android提供了本地数据库的查询uri,可以查询出数据: 采用一个AsyncQueryHandler来进行查询, AsyncQu...

本文源自: 环亚国际



上一篇:蓝牙核心技术概述(四):蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM),l2caprfocmm 下一篇:没有了