android - 未在自定义联系人光标适配器中获取电话号码

标签 android

我正在尝试实现一个自定义光标适配器来显示联系人姓名和号码。但是我只得到姓名和号码为空。请帮我弄清楚为什么我得到的号码为空。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <AutoCompleteTextView android:id="@+id/autotextContacts"
        android:completionThreshold="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

<TextView
    android:id="@+id/namelabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
/>

<TextView
    android:id="@+id/numberlabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="10sp"
    android:layout_below = "@id/namelabel"
/>

private AutoCompleteTextView destination;
private ContentResolver mContentResolver;
private static final String[] PEOPLE_PROJECTION = new String[] {
    People._ID,
    People.NAME,
    People.NUMBER


};


 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_view);
        mContentResolver = getContentResolver();
        Cursor cursor = managedQuery(
                People.CONTENT_URI, PEOPLE_PROJECTION, null,
                null, null);

    if(cursor == null ){
        Log.i(getClass().getSimpleName(),"cursor null");
    }
    CallListAdapter adapter = new CallListAdapter(this, cursor);
    destination = (AutoCompleteTextView) findViewById(R.id.autotextContacts);
    destination.setAdapter(adapter);
}


public static class CallListAdapter extends CursorAdapter implements
Filterable {
    Context context;
    private ContentResolver mContent;
    public CallListAdapter(Context context, Cursor c) {
        super(context, c, true);
        mContent = context.getContentResolver();
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view = (View) inflater.inflate(R.layout.contacts_view, parent, false);
        TextView tx = (TextView) view.findViewById(R.id.namelabel);
        tx.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView tx = (TextView) view.findViewById(R.id.rishi_label);
        tx.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
        Log.i("LOG","Contact NAME" + cursor.getString(cursor.getColumnIndex(People.NAME)));
        int numbercolumn = cursor.getColumnIndex(People.NUMBER);
        String number;
         number = cursor.getString(numbercolumn);
         Log.i("LOG", "Contact NUMBER" + number);

    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(cursor.getColumnIndex(People.NAME));
    }

    @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(Contacts.People.NAME);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }
        return mContent.query(Contacts.People.CONTENT_URI,
                PEOPLE_PROJECTION, buffer == null ? null : buffer
                        .toString(), args,
                null); 
    }
}

谢谢

最佳答案

我认为在下面的代码中您会得到答案。

if (Boolean.parseBoolean(hasPhone)) {
                        // Find all phone numbers

                        Cursor phones1 = getContentResolver().query(Phone.CONTENT_URI,
                                null, Phone.CONTACT_ID + " = " + contactId, null, null);
                        while (phones1.moveToNext()) {
                            String number = phones1.getString(phones1
                                    .getColumnIndex(Phone.NUMBER));
                            int type0 = phones1.getInt(phones1
                                    .getColumnIndex(Phone.TYPE));

                            switch (type0) {
                            case Phone.TYPE_HOME:
                                // do something with the Home number here...
                                card.Homeno = number;
                                break;
                            case Phone.TYPE_MOBILE:
                                // do something with the Mobile number here...
                                card.MobileNo = number;
                                break;
                            case Phone.TYPE_WORK:
                                // do something with the Work number here...
                                card.WorkNo = number;
                                break;
                            case Phone.TYPE_OTHER:
                                // do something with the Other number here...
                                allphones += "\nOther No. : " + number;
                                break;
                            case Phone.TYPE_CUSTOM:
                                // do something with the Other number here...
                                card.CustomNo=number;
                                break;
                            }
                        }
                        phones1.close();
                    }

关于android - 未在自定义联系人光标适配器中获取电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5645714/

相关文章:

c# - 标签在 iOS 中显示,但在 Android 中不显示

android - ListView BaseAdapter 中的 EditText 奇怪行为

安卓 : Text is not Showing in the TextView after applying Async Progress Dialog

android - 如何在android中使用tagsoup解析xml中的html内容

android - Android TabHost 示例的问题

android - AdMob 如何检测开发者点击?

android - 谷歌地图 api v2 在标记附近缩放

android - 通过 Maps API v2 处理 SurfaceView 中的触摸事件

Android 自定义通知不显示代码更改

Android:如何将样式应用于 ViewGroup 的所有子级