Tôi đang cố gắng triển khai AutoCompleteTextView tùy chỉnh để chọn số điện thoại của liên hệ từ danh sách đề xuất hiển thị tên liên hệ, loại số điện thoại và số điện thoại. Tôi đã tạo CursorAdapter tùy chỉnh xác định và đặt Bố cục và TextView của tôi cho từng đề xuất và truy vấn danh sách liên hệ dựa trên văn bản do người dùng nhập thông qua runQueryOnBackgroundThread. Tôi đang gặp sự cố trong đó các đề xuất có vẻ chính xác cho hai giá trị đầu tiên được nhập (ví dụ: "ab" đề xuất "abcd" và "abyz") nhưng không cho bất kỳ điều gì ngoài đó (ví dụ: "abc" đề xuất "abyz"). Đối với sau này, khi đề xuất "abyz" được chọn, các giá trị cho "abcd" được trả về.Android - Custom AutoCompleteTextView CursorAdaptor - Hành vi đề xuất
Mã cho hoạt động chính:
final ContactInfo cont = new ContactInfo(ctx);
Cursor contacts = cont.getContacts2(null);
startManagingCursor(contacts);
ContactsAutoCompleteCursorAdapter adapter = new ContactsAutoCompleteCursorAdapter(this, contacts);
mPersonText.setAdapter(adapter);
mPersonText.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));
mPersonNum.setText(number);
}
});
Mã cho lớp địa chỉ liên lạc của tôi mà trả về một con trỏ cho tất cả các địa chỉ liên lạc:
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 = ctx.getContentResolver().query(uri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
return people;
}
Mã cho CursorAdapter tôi:
public class ContactsAutoCompleteCursorAdapter extends CursorAdapter implements Filterable {
private TextView mName, mType, mNumber;
private ContentResolver mContent;
public ContactsAutoCompleteCursorAdapter(Context context, Cursor c) {
super(context, c);
mContent = context.getContentResolver();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final LayoutInflater mInflater = LayoutInflater.from(context);
final View ret = mInflater.inflate(R.layout.contacts_auto_list, null);
mName = (TextView) ret.findViewById(R.id.name);
mType = (TextView) ret.findViewById(R.id.phonetype);
mNumber = (TextView) ret.findViewById(R.id.phonenum);
return ret;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
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);
mName.setText(name);
if (type == 1) {mType.setText("Home");}
else if (type == 2) {mType.setText("Mobile");}
else if (type == 3) {mType.setText("Work");}
else {mType.setText("Other");}
mNumber.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};
return mContent.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");
}
}
Như tôi đã nói ở trên, khi người dùng nhập "ab" vào AutoCompleteTextView các đề xuất là "abcd" và "abyz", tuy nhiên khi người dùng gõ "abc" đề xuất chỉ là "abyz". Khi người dùng chọn "abyz" trong trường hợp đó, các giá trị cho "abcd" được trả về. Dưới đây là hai ảnh chụp màn hình cho thấy những gì tôi đang cố gắng để mô tả:
Tôi đã đọc mọi câu hỏi tôi có thể tìm thấy ở đây và các nơi khác nhưng dường như không thể con số này ra. Tôi khá mới để phát triển Android vì vậy tôi xin lỗi trước nếu sai lầm của tôi là một đơn giản. Cảm ơn trước!