如何既DISPLAY_NAME和数量定制CursorAdapter的结合? [英] how to combine both DISPLAY_NAME and NUMBER in a customized CursorAdapter?

查看:159
本文介绍了如何既DISPLAY_NAME和数量定制CursorAdapter的结合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

与API的演示,我只能把DISPLAY_NAMEs在结果光标。我不知道如何既姓名和号码组合成一个光标。谢谢!

从API演示codeS:

  ContentResolver的内容= getContentResolver();
光标光标= content.query(ContactsContract.Contacts.CONTENT_URI,
    PEOPLE_PROJECTION,NULL,NULL,NULL);
ContactListAdapter适配器=新ContactListAdapter(这一点,光标);
mAutoCompleteTextView.setAdapter(适配器);私有静态类ContactListAdapter CursorAdapter的扩展实现的可筛选{
    私人ContentResolver的MCR;    公共ContactListAdapter(上下文的背景下,光标C){
        超(背景下,C);
        MCR = context.getContentResolver();
    }    @覆盖
    公共查看NewView的(上下文的背景下,光标光标的ViewGroup父){
        最后LayoutInflater吹气= LayoutInflater.from(背景);
        最后TextView的视图=(TextView中)inflater.inflate(
                android.R.layout.simple_dropdown_item_1line,父母,假);
        view.setText(cursor.getString(1));
        返回视图。
    }    @覆盖
    公共无效bindView(查看视图,上下文的背景下,光标光标){
        ((的TextView)视图).setText(cursor.getString(1));
    }    @覆盖
    公共字符串convertToString(光标光标){
        返回cursor.getString(1);
    }    @覆盖
    公共光标runQueryOnBackgroundThread(CharSequence的约束){
        如果(getFilterQueryProvider()!= NULL){
            返回getFilterQueryProvider()runQuery(约束)。
        }
        StringBuilder的缓冲= NULL;
        字串[] args = NULL;
        如果(约束!= NULL){
            缓冲区=新的StringBuilder();
            buffer.append(UPPER();
            buffer.append(ContactsContract.Contacts.DISPLAY_NAME);
            buffer.append()GLOB?);
            ARGS =新的String [] {constraint.toString()与toUpperCase()+*};
        }
        返回mCR.query(ContactsContract.Contacts.CONTENT_URI,PEOPLE_PROJECTION,
                缓冲== NULL?空:buffer.toString(),指定参数时,NULL);
    }
}私有静态最后的String [] = PEOPLE_PROJECTION新的String [] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME,
    ContactsContract.Contacts.HAS_PHONE_NUMBER
};


解决方案

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

 最终长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名称列= cursor.getColumnIndex(People.NAME);
            INT numberKeyColumn = cursor.getColumnIndex(People.NUMBER_KEY);
            phoneNumber的= this.getPhoneNumbers(ID); }

然后getPhoneNumbers方法,在相同的类中,unforunately必须再次遍历。如果有一个更好的方式通过结合光标(新API)来做到这一点,但它的作品不能肯定。

 众长getPhoneNumbers(字符串ID){
        长数=新龙(0);
        ContentResolver的CR = this.context.getContentResolver();
        光标pCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                空值,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,
                新的String [] {ID},NULL);
        pCur.moveToFirst();
        如果(pCur.getCount()大于0){
            字符串NUM = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            NUM = num.replaceAll( - ,);
            数=的Long.parseLong(NUM);
        }
        pCur.close();
        返回数;
    }

I want to load all of my contacts' names and phone numbers into an AutoCompleteTextView's adapter. How can I achieve that? for example, when i type "G", it will show "Good, <111111>", "Good, <222222>" in its drop-down list.

with the api demo, i can only put the DISPLAY_NAMEs in the result cursor. I don't know how to combine both names and numbers into one cursor. thanks!

codes from the api demo:

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

解决方案

The numbers should already be part of the cursor. Retrieve them like so.

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

Then the getPhoneNumbers method, in the same class, unforunately has to iterate again. Not sure if there's a better way to do this by binding to a cursor (in the new api), but it works.

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

这篇关于如何既DISPLAY_NAME和数量定制CursorAdapter的结合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆