0

i'm new to android and probably this is silly question but please help. i am receiving output as

contact Name: RRRR
phone Number: XXXXXXXXX
contact Name: SSSS
phone Number: YYYYYYYYY
phone Number: aaaaaaaaa
phone Number: zzzzzzzzz
contact Name: TTTT
phone Number: XXXXXXXXX
phone Number: ccccccccc . . .

//code

public void readContacts() { 
StringBuffer sb = new StringBuffer(); 
sb.append("......Contact Details....."); 
ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String name = null;

if (cur.getCount() > 0) {
    while (cur.moveToNext()) {
        String 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) { 
            System.out.println("name : " + name + ", ID : " + id); sb.append("\n Contact Name:" + name);
            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));
            sb.append("\n Phone number:" + phone+":"+pCur.getCount()); System.out.println("phone" + phone);
            }pCur.close();
        }
        outputText.setText(sb);
    }

}

}

P.S: how can i store the ouput in container and then bundle it and send to server ?

thanks in advance,

1 Answer 1

1

I tried to put list of contacts in JSONArray,

Step-1 : Declare JSONArray finalJarray; for in future use to send JSONArray to server.

Step-2 : Generate JSONArray with contact name and multiple phone number. Unique key name to multiple phone numbers so that it can not override.

    public void readContacts() {
    try {
        finalJarray = new JSONArray();

        StringBuffer sb = new StringBuffer();
        sb.append("......Contact Details.....");
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        String phone = null;
        String name = null;

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                JSONObject contactObject = new JSONObject();

                String 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) {
                    System.out.println("name : " + name + ", ID : " + id);
                    sb.append("\n Contact Name:" + name);

                    contactObject.put("contact_name", name);

                    Cursor pCur = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = ?", new String[] { id },
                                    null);
                    int tmpPhoneCount = 0;
                    while (pCur.moveToNext()) {
                        phone = pCur
                                .getString(pCur
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        sb.append("\n Phone number:" + phone + ":"
                                + pCur.getCount());
                        System.out.println("phone" + phone);
                        contactObject.put("phone_number_"+tmpPhoneCount, phone);
                        tmpPhoneCount++;
                    }
                    pCur.close();
                   finalJarray.put(contactObject);
                }
                // outputText.setText(sb);
            }
        }
        System.out.println("finalJarray.toString() = "
                + finalJarray.toString());
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

I tried on my device, let me know if any query.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks chitrang but contactObject has only names and no phone number
I tried on my device and, it has contact name as well multiple phone number too. Are you able to see log cat, then send it.
yeah just one line was above the if condition so i was getting that now its working fine thank you and there is empty set in array eg: [{},{},{"phone_number_0":"ffffff","contact_name":"aaaaaa"},{},{}] can u please tel how to remove this empty set
I have edited the answer, 1) avoid the counter 2) same as you did, put this finalJarray.put(contactObject); inside if. To avoid special case like 'ffffff' you need to mention if statement before adding to JSONArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.