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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值