0

I am using the following code and I am getting multiple names and phone number for the same person. How can I get single name and only mobile numbers of all users whose number I have dialed.

Code:

     if (android.os.Build.VERSION.SDK_INT >= 21) {
                mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.CONTACT_LAST_UPDATED_TIMESTAMP + " DESC");
            }else{
                mCursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Contacts.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", null, ContactsContract.Data.LAST_TIME_CONTACTED + " DESC");
            }
            int number = mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int name = mCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);

            while (mCursor.moveToNext()) {
                String phName = mCursor.getString(name);
                String phNumber = mCursor.getString(number);
    }                       

}

updated code:

 ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String 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.Data.CONTENT_URI,
                            new String[]{ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.LABEL},
                            ContactsContract.Data.CONTACT_ID + "=?" + " AND "
                                    + ContactsContract.Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
                            new String[]{String.valueOf(id)}, null);


                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        Log.w("num", "Name: " + name + ", Phone No: " + phoneNo);
                    }
                    pCur.close();
                }
            }
        }
jason
  • 3,932
  • 11
  • 52
  • 123
  • don't use managedQuery. Try this solution instead http://stackoverflow.com/questions/12562151/android-get-all-contacts/12562234#12562234 – drulabs Feb 18 '16 at 06:00

1 Answers1

0

Instead of manageQuery, you should use following with specific filed which you want to keep in your SELECT query. Below is just example, you can refer and use as per your requirement:

Cursor c = getContentResolver().query(Data.CONTENT_URI,
                                      new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
                                      Data.CONTACT_ID + "=?" + " AND "
                                              + Data.MIMETYPE + "= + Phone.CONTENT_ITEM_TYPE + ",
                                      new String[] {String.valueOf(contactId)}, null);
Rakesh
  • 756
  • 1
  • 9
  • 19
  • I am getting error: Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: SELECT _id, data1, data2, data3 FROM view_data data LEFT OUTER JOIN (SELECT data_usage_stat.data_id as STAT_DATA_ID, SUM(data_usage_stat.times_used) as times_used, MAX(data_usage_stat.last_time_used) as last_time_used FROM data_usage_stat GROUP BY data_usage_stat.data_id) as data_usage_stat ON (STAT_DATA_ID=data._id) WHERE (1) AND ((contact_id=? AND mimetype= + Phone.CONTENT_ITEM_TYPE + )) – jason Feb 18 '16 at 06:41
  • I have updated code in Q .Please look into it .Thanks – jason Feb 18 '16 at 06:50
  • why is it crashing ? – jason Feb 18 '16 at 07:18