1

That's my code , which give me all number of my android device like mobile numbers, whatsapp numbers and landline numbers. But i want only mobile number and landline numbers from android phone book into my application. How can i get only mobile number and landline number of every contact name?

private void addContactsInList() {
    // TODO Auto-generated method stub

    Cursor cursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null, null, null, null);

    try {
        ContactsListClass.phoneList.clear();
    } catch (Exception e) {

    }

    while (cursor.moveToNext()) {
        String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        int phoneId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));


        Contact cp = new Contact();

        cp.setName(phoneName);
        cp.setNumber(phoneNumber);
        cp.setId(phoneId);


        ContactsListClass.phoneList.add(cp);
       // Log.e("add to list", "content" + phoneId +phoneName +phoneNumber);
    }
    cursor.close();

    lv = new ListView(context);

    lv.setLayoutParams(new LayoutParams(
            LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT));


    llContainer.addView(lv);

    Collections.sort(ContactsListClass.phoneList, new Comparator<Contact>() {
        @Override
        public int compare(Contact lhs,
                           Contact rhs) {
            return lhs.getName().compareTo(
                    rhs.getName());
        }
    });

    contactAdapter = new ContactsAdapter(MainActivity.this,
            ContactsListClass.phoneList);
    lv.setAdapter(contactAdapter);
brennanyoung
  • 6,243
  • 3
  • 26
  • 44
Ashish Patel
  • 59
  • 1
  • 9

2 Answers2

2

inside your while loop, put something like this:

    while (phoneCursor.moveToNext()) {
                phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                //String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                switch (type) {
                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                        // do something with the Home number here...
                        break;
                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                        output.append("\n Phone number:" + phoneNumber);
                        break;
                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                        // do something with the Work number here...
                        break;
                }
            }

as seen in this answer

Community
  • 1
  • 1
Shine
  • 3,788
  • 1
  • 36
  • 59
0
   Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
   Uri phonesUri = Uri.withAppendedPath(personUri,                   People.Phones.CONTENT_DIRECTORY);
   String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
        Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);
    if(cursor != null && cursor.moveToFirst()){
    do{
    String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    switch (type) {

                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                        break;   
                    }
    }while(cursor.moveToNext());
    }
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32