gpt4 book ai didi

Android - 自定义 AutoCompleteTextView CursorAdaptor - 建议行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:11 26 4
gpt4 key购买 nike

我正在尝试实现自定义 AutoCompleteTextView,以便从显示联系人姓名、电话号码类型和电话号码的建议列表中选择联系人的电话号码。我创建了一个自定义 CursorAdapter,它为每个建议定义和设置我的 Layout 和 TextView,并根据用户通过 runQueryOnBackgroundThread 输入的文本查询联系人。我遇到了一个问题,对于输入的前两个值,建议似乎是正确的(例如,“ab”建议使用“abcd”和“abyz”),但对于除此之外的任何其他值(例如,“abc”建议使用“abyz”),都不正确。 For the latter, when the "abyz"suggestion is selected, the values for "abcd"are returned.

主要 Activity 代码:

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);
}
});

返回所有联系人游标的联系人类代码:

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;
}

我的 CursorAdapter 代码:

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");
}

正如我上面所说,当用户在 AutoCompleteTextView 中输入“ab”时,建议是“abcd”和“abyz”,但是当用户键入“abc”时,建议只是“abyz”。在这种情况下,当用户选择“abyz”时,将返回“abcd”的值。下面是两个屏幕截图,显示了我要描述的内容:

enter image description here enter image description here

我已经阅读了在这里和其他地方可以找到的所有问题,但似乎无法解决这个问题。我是 Android 开发的新手,所以如果我的错误很简单,我提前道歉。提前致谢!

最佳答案

经过更多研究,我似乎已经回答了我自己的问题。将我的 textView 的 View 设置从 newView 函数移动到 bindView 函数似乎已经成功了,我认为这是有道理的......

@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);

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 = (TextView) view.findViewById(R.id.name);
mType = (TextView) view.findViewById(R.id.phonetype);
mNumber = (TextView) view.findViewById(R.id.phonenum);

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);
}

关于Android - 自定义 AutoCompleteTextView CursorAdaptor - 建议行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10392325/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com