我想AutoCompleteTextView
在我的应用程序中添加一个并按名称和号码搜索联系人,就像在带有 android 的本机 SMS 应用程序中所做的那样。我已经在互联网上查看并尝试了很多东西,但我希望我的应用程序将其完全显示为 android SMS 应用程序。这是我正在尝试仅搜索的代码Display_Name
。
public class MakePayment extends Activity {
private AutoCompleteTextView mAuto;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.makepayment);
mAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextViewTest);
ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
mAuto.setAdapter(adapter);
}
public static class ContactListAdapter extends CursorAdapter implements Filterable {
public ContactListAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater inflater = LayoutInflater.from(context);
final TextView view = (TextView) inflater.inflate(
android.R.layout.simple_expandable_list_item_1, parent, false);
view.setText(cursor.getString(2));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(2));
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(2);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return mContent.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args,
null);
}
private ContentResolver mContent;
}
private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME,
};
}
我如何也可以通过电话号码进行搜索?然后像这样显示它: