联系人获取两次

时间:2014-10-15 15:12:55

标签: android android-contacts

我在我的应用程序中使用以下代码来获取列表视图中的联系人,但它显示相同的联系人两次。

   Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
                while (phones.moveToNext())
                { 
                  String name1=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                 // System.out.println(".................."+name1); 
}

我认为它获得了sim +内部联系人。有没有办法只通过内部存储来获取联系人?

1 个答案:

答案 0 :(得分:0)

我使用了这种方法,只显示了一次

// declear两个数组列表

List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();

getAllContacts(this.getContentResolver());

//在您想要获取联系人的位置调用此处并在mainfest中授予权限

public void getAllContacts(ContentResolver cr) {

    Cursor phones = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (phones.moveToNext()) {
        String name = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = phones
                .getString(phones
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        // Bitmap bitmap=-
        // phones.getBlob(ContactsContract.CommonDataKinds.Phone.)
        System.out.println("" + phoneNumber);

        name1.add(name);
        phno1.add(phoneNumber);
    }

    phones.close();
}

//像这样设置自适应

class MyAdapter extends BaseAdapter {

    LayoutInflater mInflater;
    TextView tv1, tv;
    CheckBox cb;

    MyAdapter() {

        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {

        return name1.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

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

        View vi = convertView;
        if (convertView == null)
            vi = mInflater.inflate(R.layout.friends_addmobile_contacts,
                    null);
        TextView tv = (TextView) vi.findViewById(R.id.tvMcontactname);
        tv1 = (TextView) vi.findViewById(R.id.tvMcontactphoneno);

        tv.setText("" + name1.get(position));
        tv1.setText("" + phno1.get(position));
        // tv1.setText("Phone No :" + phno1.get(position));
        return vi;
    }

}

//调用此方法

    MyAdapter ma = new MyAdapter();
    lvmobilecontacts.setAdapter(ma);