Android 聯系人模糊查詢


相信很多人都做過聯系人的增刪改查,今天嘗試着做了一下,發現其實並沒有那么容易。我想做實現一個 根據姓名,電話和email聯合查詢的

這么一個功能。


話不多說 直接上代碼

首先 先把讀和寫權限給開了

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />


// 如果value 值為空,查詢所有


String _value = editBox.getText().toString(); //要查詢的字段
String[] typeStrings;
if (v.getId() == R.id.btn1)
typeStrings = new String[] { "name" };
else if (v.getId() == R.id.btn2) {
typeStrings = new String[] { "name", "phone" };
} else if (v.getId() == R.id.btn3) {
typeStrings = new String[] { "name", "email" };
} else {
typeStrings = new String[] { "name", "phone", "email" };
}


// 查詢的條件 ,支持 name,phone,email 模糊查詢
String _selection = null;
String[] _selectionArray = null;
if (!TextUtils.isEmpty(_value)) {
if (null != typeStrings && typeStrings.length > 0) {
_selectionArray = new String[typeStrings.length];
for (int i = 0; i < typeStrings.length; i++) {
_selectionArray[i] = typeStrings[i];
}
} else {
_selectionArray = new String[] { NAME, PHONE, EMAIL };
}
}

//這個是重點
if (_selectionArray != null) {
StringBuffer _tempStr = new StringBuffer();
for (String _str : _selectionArray) {
if (NAME.equals(_str)) {
_tempStr.append(" || " + "(" + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE + "'" + " AND "
+ ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " LIKE " + "'%" + _value + "%'" + ")");
}
if (PHONE.equals(_str)) {
_tempStr.append(" || " + "(" + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'" + " AND "
+ ContactsContract.CommonDataKinds.Phone.DATA + " LIKE " + "'%" + _value + "%'" + ")");
}
if (EMAIL.equals(_str)) {
_tempStr.append(" || " + "(" + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'" + " AND "
+ ContactsContract.CommonDataKinds.Email.DATA + " LIKE " + "'%" + _value + "%'" + ")");
}
}
_selection = _tempStr.substring(4).toString();
}


ContentResolver _contentResolver = getApplicationContext().getContentResolver();
// 查詢的字段 raw_contact_id
String[] _projection = { ContactsContract.Data.RAW_CONTACT_ID };
Cursor _cursor = _contentResolver.query(ContactsContract.Data.CONTENT_URI, _projection, _selection, null, null);


List<String> _ids = null;
if (_cursor != null && _cursor.getCount() > 0) {
_ids = new ArrayList<String>();
while (_cursor.moveToNext()) {
_ids.add(_cursor.getString(0));
}
_cursor.close();
}


if (null != _ids && _ids.size() > 0) {
StringBuffer _idStr = new StringBuffer("( ");
StringBuffer _tempStr = new StringBuffer();
for (String _id : _ids) {
_tempStr.append("," + _id);
}
_idStr.append(_tempStr.substring(1));
_idStr.append(" )");
_selection = ContactsContract.Data.RAW_CONTACT_ID + " IN " + _idStr.toString();
}


// 查詢的字段 raw_contact_id, mimetype_id , data1
_projection = new String[] { ContactsContract.Data.RAW_CONTACT_ID, ContactsContract.Data.MIMETYPE, ContactsContract.Data.DATA1 };
_cursor = _contentResolver.query(ContactsContract.Data.CONTENT_URI, _projection, _selection, null, null);


Map<String, DoContactBean> datas = new HashMap<String, DoContactBean>();
List<DoContactBean> contacts = new ArrayList<DoContactBean>();
if (_cursor != null && _cursor.getCount() > 0) {
while (_cursor.moveToNext()) {
String rawContactId = _cursor.getString(0);
String mimetypeId = _cursor.getString(1);
String data1 = _cursor.getString(2);
DoContactBean bean = datas.get(rawContactId);
if (null == bean) {
bean = new DoContactBean();
bean.setPhones(new ArrayList<String>());
bean.setEmails(new ArrayList<String>());
bean.setId(rawContactId);
datas.put(rawContactId, bean);
contacts.add(bean);
}


if (mimetypeId.contains("/name")) {
bean.setName(data1);
} else if (mimetypeId.contains("/email")) {
bean.getEmails().add(data1);
} else if (mimetypeId.contains("/phone")) {
bean.getPhones().add(data1);
}
}
_cursor.close();
}


JSONArray _array = new JSONArray();
for (DoContactBean bean : contacts) {
try {
_array.put(bean.toJsonObject());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
textView.setText(_array.toString());


XML代碼


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <EditText
        android:id="@+id/box"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="hint" >
    </EditText>


    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getAll" />


    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getName" />


    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getName_Phone" />


    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="getName_Email" />


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com