联系人列表显示为空

时间:2016-05-11 08:21:36

标签: java android contacts custom-lists

我想显示来自手机通讯录的自定义联系人列表。我想获取联系人列表,我必须从列表中选择多个联系人。

为此,我创建了一个自定义视图。自定义视图的适配器。

此外,我还可以通过手机联系人获取联系人。它使用光标来获取联系人。但是光标显示为null,列表也显示为空。

当我调试代码时,它显示游标为null并且不会向前移动。

我还为清单中的联系人添加了红色权限。

但我无法获得名单。

ContactList活动:

    public class ContactList extends ListActivity {
    /** Called when the activity is first created. */

    private ArrayList<contact> contact_list = null;
    private contactAdapter mContactAdapter = null;

    private ArrayList<contact> items;
    boolean[] isChecked;
    Cursor mCursor;
    ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        contact_list = new ArrayList<contact>();
        lv = getListView();

                getContacts();

    }

    @SuppressWarnings("unused")
    private void getContacts() {

        try {

            String[] projection = new String[] {
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    ContactsContract.Contacts._ID };

            mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                    projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
                    ContactsContract.Contacts.DISPLAY_NAME);

            while (mCursor.moveToNext()) {
                contact contact = new contact();

                String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
                contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                contact_list.add(contact);
            }
            isChecked = new boolean[mCursor.getCount()];

            for (int i = 0; i < isChecked.length; i++) {
                isChecked[i] = false;
            }

            this.mContactAdapter = new contactAdapter(this, R.layout.listview, contact_list);
            lv.setAdapter(this.mContactAdapter);
            mCursor.close();

        } catch (Exception e) {
            Log.d("getContacts", e.getMessage());
        }
    }

    public class contactAdapter extends ArrayAdapter<contact> {

        public contactAdapter(Context context, int textViewResourceId, ArrayList<contact> items1) {
            super(context, textViewResourceId, items1);
            items = items1;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder mViewHolder;

            mViewHolder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview, parent, false);
            mViewHolder.cb = (CheckBox) convertView.findViewById(R.id.checkBox);

            convertView.setTag(mViewHolder);

            if (isChecked[position] == true)
                mViewHolder.cb.setChecked(true);
            else
                mViewHolder.cb.setChecked(false);
            mViewHolder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
                    if (buttonView.isChecked())
                        isChecked[position] = true;
                    else
                        isChecked[position] = false;
                }
            });

            contact contacts = items.get(position);
            if (contacts != null) {
                if (mViewHolder.cb != null) {
                    mViewHolder.cb.setText(contacts.getContactName());
                }
            }

            return convertView;
        }
    }

    public class ViewHolder {
        CheckBox cb;
    }

}

自定义列表视图

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="?android:attr/listPreferredItemHeight">
    <ImageView
        android:id="@+id/contactimage"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginRight="8dp"

        android:contentDescription="@string/app_name"
        android:scaleType="centerInside" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_toLeftOf="@+id/contactcheck"
        android:layout_toRightOf="@+id/contactimage" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Contact Name"
            android:textColor="#000"
            android:textIsSelectable="false"
            android:textSize="18dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:singleLine="true"
            android:text="09876543210"
            android:textColor="#2689e0"
            android:textIsSelectable="false"
            android:textSize="14dp" />
    </RelativeLayout>

    <CheckBox
        android:id="@+id/contactcheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="8dp" />

</RelativeLayout>

和联系班:

public class contact {

    private String contactName;

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public int getContactid() {
        return contactid;
    }

    public void setContactid(int contactid) {
        this.contactid = contactid;
    }

    private int contactid;

}

出了什么问题?

有人可以帮忙吗?谢谢..

2 个答案:

答案 0 :(得分:0)

  

在开始阅读表之前,首先将光标移动到头部,在联系人列表活动中写入while语句之前添加以下行

mCursor.movetoFirst();//Add this line

答案 1 :(得分:0)

try to this:-

          ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
          String phone = null;
          String id = null;
          String name = null;
          if (cur.getCount() > 0) 
          {
           while (cur.moveToNext()) 
           {
            id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
            {
             Cursor pCur = cr.query(
               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
               null,
               ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                 + " = ?", new String[] { id }, null);
             while (pCur.moveToNext()) 
             {
              phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
             }
             pCur.close();

            }
           }
           }