您好我正在开发一个应用来搜索联系人。搜索字符串可以是数字或联系人姓名。我可以根据联系人姓名搜索列表。如果用户输入联系人号码,则不确定如何搜索相同内容。这是我正在使用的代码
Cursor contacts = getContacts2(null);
startManagingCursor(contacts);
TestForCursorAdapter adapter = new TestForCursorAdapter(this, contacts);
mTxtPhoneNo.setAdapter(adapter);
mTxtPhoneNo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Cursor cursor = (Cursor) arg0.getItemAtPosition(arg2);
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
mTxtPhoneNo.setText(number);
}
});
public Cursor getContacts2(String where)
{
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = this.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
return people;
}
public class TestForCursorAdapter extends CursorAdapter implements Filterable
{
ImageView imageAvatar;
ContentResolver contentResolver;
LayoutInflater layoutInflater;
ArrayList<String> arrofnumberfrominbuiltsms = new ArrayList<String>();
ArrayList<String> arrofnumbettocomapreandbold = new ArrayList<String>();
String recipient_ids;
String r_address;
String contactName;
String contactID;
String bold= "false";
private String snippet;
private long timestamp;
private String dateString;
private String timeString;
String threadid;
private String phoneNumber;
private String numberType;
private ContentResolver mContext;
@SuppressWarnings("deprecation")
public TestForCursorAdapter(Context context, Cursor people)
{
super(context, people);
layoutInflater = LayoutInflater.from(context);
mContext = context.getContentResolver();
}
@Override
public View newView(Context context, Cursor people, ViewGroup parent)
{
final LayoutInflater mInflater = LayoutInflater.from(context);
final View ret = mInflater.inflate(R.layout.custcontview, null);
return ret;
}
@SuppressLint("SimpleDateFormat")
@Override
public void bindView(View vi, Context context, Cursor cursor)
{
TextView cname = (TextView)vi.findViewById(R.id.ccontName);
TextView cnumber = (TextView)vi.findViewById(R.id.ccontNo);
TextView ctype = (TextView)vi.findViewById(R.id.ccontType);
int nameIdx = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int typeIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int numberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String name = cursor.getString(nameIdx);
int type = cursor.getInt(typeIdx);
String number = cursor.getString(numberIdx);
cname.setText(name);
if (type == 1)
{
ctype.setText("Home");
}
else if (type == 2)
{
ctype.setText("Mobile");
}
else if (type == 3)
{
ctype.setText("Work");
}
else
{
ctype.setText("Other");
}
cnumber.setText(number);
}
@Override
public String convertToString(Cursor cursor)
{
int nameCol = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String name = cursor.getString(nameCol);
return name;
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint)
{
// this is how you query for suggestions
// notice it is just a StringBuilder building the WHERE clause of a cursor which is the used to query for results
if (getFilterQueryProvider() != null)
{
return getFilterQueryProvider().runQuery(constraint);
}
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
if (( constraint).toString().matches("[0-9]+") && constraint.length() > 2)
{//This is what I want to try
return mContext.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
"UPPER(" + ContactsContract.CommonDataKinds.Phone.NUMBER + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
else
{
return mContext.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
"UPPER(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") LIKE '" + constraint.toString().toUpperCase() + "%'", null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}
}
}
请帮助。感谢
答案 0 :(得分:0)
你只需要使用
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, searchString);
Android使用像这样的查找网址做得很好