Contacts with multiple numbers are shown multiple times in listview

时间:2015-06-15 14:58:01

标签: android listview contactscontract

i am trying to create an application to send contacts via Bluetooth. When i list the contacts contacts with multiple phone numbers are shown multiple times. How to avoid that. my code is below

public class MainActivity extends Activity implements OnClickListener {

ListView conListView = null;
private ArrayList<PhoneBean> phoneBeans;
ArrayList<String> numbers = null;
Button sendButton = null;
Button exitButton = null;
boolean flag = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    conListView = (ListView) findViewById(R.id.contactsList);
    phoneBeans = new ArrayList<PhoneBean>();
    loadContacts();
    ContactAdapter adapter = new ContactAdapter(phoneBeans, this);
    conListView.setAdapter(adapter);
    sendButton = (Button) findViewById(R.id.sendButton);
    exitButton = (Button) findViewById(R.id.exitButton);
    sendButton.setOnClickListener(this);
    exitButton.setOnClickListener(this);
}

private void loadContacts() {
    ContentResolver cr = this.getContentResolver();
    Cursor cursor = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'", null,
            ContactsContract.Contacts.DISPLAY_NAME
                    + " COLLATE LOCALIZED ASC");
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            PhoneBean bean = new PhoneBean();
            String id = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
            String name = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            Cursor pCur = cr.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                            + " = " + id, null, null);
            numbers = new ArrayList<String>();
            while (pCur.moveToNext()) {
                String phone = pCur
                        .getString(pCur
                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                numbers.add(phone);
                // Toast.makeText(this, phone, Toast.LENGTH_SHORT).show();
            }
            pCur.close();
            bean.setName(name);
            bean.setId(id);
            bean.setNumber(numbers);
            phoneBeans.add(bean);
        }
    }
}

0 个答案:

没有答案