【问题标题】:Android: Obtaining Contact List via CursorLoader - Name/Number/NumberTypeAndroid:通过 CursorLoader 获取联系人列表 - Name/Number/NumberType
【发布时间】:2016-02-20 22:37:33
【问题描述】:

我正在加载电话的联系人列表以与 AutoCompleteTextView 一起使用,我想通过测试显示电话类型(工作、家庭等),但现在我只是返回数字类型 ID(1、2、3 )

我如何获得名称:这是下面的代码 - 感谢所有帮助。

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]
        {
        ContactsContract.CommonDataKinds.Phone._ID,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
        ContactsContract.CommonDataKinds.Phone.TYPE
};

public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
    try
    {
        String SELECTION = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + ContactsContract.Contacts.DISPLAY_NAME + " != '' )" + "AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1))";
        //        + ContactsContract.Contacts.DISPLAY_NAME + " like 'p%' )" + "AND ("+ContactsContract.Contacts.HAS_PHONE_NUMBER +"=1))";

        if (mSelection.length() > 0) {
            SELECTION = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                    + ContactsContract.Contacts.DISPLAY_NAME + " like '" + mSelection + "%' )" + "AND (" + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1))";
        }

        return new CursorLoader(this, ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                CONTACTS_SUMMARY_PROJECTION, SELECTION, null,
                " DISPLAY_NAME ASC");
    }
    catch(Exception e)
    {
        Log.d(TAG, "onCreateLoader:Exception:" + R.string.Exception + ":" + e.getMessage());
    }
    return null;
}

//光标适配器是这样设置的:

            mAdapter = new SimpleCursorAdapter(this, R.layout.contactview, null,
                    new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
                    new int[]{R.id.ccontName, R.id.ccontNo, R.id.ccontType}, 0);
            mTxtPhoneNo.setAdapter(mAdapter);

【问题讨论】:

  • 听起来您可能正在获取 id #(如果这是正确的名称)。您是否尝试过附加.getString(), getLong() 等。使用光标,我已经使用过您按照csr(&lt;index&gt;&gt;).getString(); 的方式执行某些操作,其中 index 是从 0 开始的数字偏移量。

标签: android


【解决方案1】:

您可以使用Phone.getTypeLabel() 获取电话的正确(本地化)标签。在这种情况下,您的查询还应该选择Phone.LABEL 列,并且该列的值应该作为第三个参数传递给上述方法(如果在光标行中找到的类型是TYPE_CUSTOM,则@987654326 @column 包含实际标签)。

static final String[] PROJECTION = {Phone._ID, Phone.DISPLAY_NAME, 
        Phone.NUMBER, Phone.TYPE, Phone.LABEL};
...
int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
String label = cursor.getString(cursor.getColumnIndex(Phone.LABEL));
CharSequence resolvedLabel = Phone.getTypeLabel(mResources, type, label);

编辑

首先,您更新后的代码仍然没有在投影中包含Phone.LABEL。也许这是您编辑时的疏忽,但无论如何,您都需要选择该列。

其次,SimpleCursorAdapter 只是将列的值转储到相应的视图中。正如我在上面演示的那样,这不足以显示正确的标签,除了SimpleCursorAdapter 提供的逻辑之外,您还需要额外的逻辑。

您可以编写自己的类扩展 CursorAdapter 并将该逻辑包含在 bindView() 中,或者您可以使用 SimpleCursorAdapter.setViewBinder() 并提供如下实现:

mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
    @Override
    public boolean onBindView(View view, Cursor cursor, int columnIndex) {
        if (view.getId() == R.id.ccontType) {
            // this view needs the label
            int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
            String label = cursor.getString(cursor.getColumnIndex(Phone.LABEL));
            CharSequence resolvedLabel = Phone.getTypeLabel(view.getResources(), type, label);
            ((TextView) view).setText(resolvedLabel);

            return true; // we've handled the binding for the view
        }

        return false; // allow adapter to use it's own basic logic
    }
});

【讨论】:

  • 很抱歉,我仍然无法让它工作 - 我在使用旧光标时拉出了 Type in name 表单,但是如何编辑 cursorloader 以设置正确的标签。我用我的简单光标适配器更新了上面的代码...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-04
  • 2016-10-08
  • 1970-01-01
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 2012-11-26
相关资源
最近更新 更多