gpt4 book ai didi

android - 如何在自定义的 CursorAdapter 中组合 DISPLAY_NAME 和 NUMBER?

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:05 25 4
gpt4 key购买 nike

我想将所有联系人的姓名和电话号码加载到 AutoCompleteTextView 的适配器中。我怎样才能做到这一点?例如,当我输入“G”时,它会在下拉列表中显示“Good, <111111>”、“Good, <222222>”。

在 api 演示中,我只能将 DISPLAY_NAME 放入结果游标中。我不知道如何将名称和数字组合成一个光标。谢谢!

来自 api 演示的代码:

ContentResolver content = getContentResolver();
Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI,
PEOPLE_PROJECTION, null, null, null);
ContactListAdapter adapter = new ContactListAdapter(this, cursor);
mAutoCompleteTextView.setAdapter(adapter);

private static class ContactListAdapter extends CursorAdapter implements Filterable {
private ContentResolver mCR;

public ContactListAdapter(Context context, Cursor c) {
super(context, c);
mCR = 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_dropdown_item_1line, parent, false);
view.setText(cursor.getString(1));
return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view).setText(cursor.getString(1));
}

@Override
public String convertToString(Cursor cursor) {
return cursor.getString(1);
}

@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 mCR.query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION,
buffer == null ? null : buffer.toString(), args, null);
}
}

private static final String[] PEOPLE_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
};

最佳答案

数字应该已经是光标的一部分。像这样检索它们。

final long phoneNumber;// = c.getColumnIndex(People.NUMBER_KEY);
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){
id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
int nameColumn = cursor.getColumnIndex(People.NAME);
int numberKeyColumn = cursor.getColumnIndex(People.NUMBER_KEY);
phoneNumber = this.getPhoneNumbers(id); }

然后 getPhoneNumbers 方法,在同一个类中,不幸的是不得不再次迭代。不确定是否有更好的方法通过绑定(bind)到游标(在新的 api 中)来执行此操作,但它有效。

        public Long getPhoneNumbers(String id) {
Long number = new Long(0);
ContentResolver cr = this.context.getContentResolver();
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
pCur.moveToFirst();
if (pCur.getCount() > 0){
String num = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
num = num.replaceAll("-", "");
number = Long.parseLong(num);
}
pCur.close();
return number;
}

关于android - 如何在自定义的 CursorAdapter 中组合 DISPLAY_NAME 和 NUMBER?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4358106/

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