1
   ArrayList<String> contacts_list = new ArrayList<String>();

        Cursor c = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);
        while (c.moveToNext()) {

            contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            PhoneNumber = c
                    .getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contacts_list.add("ContactName:"+contactName + "\n" +"ContactNumber:"+PhoneNumber );

        }

        c.close();

In above code,getting the name and phone number but not separating the arraylist of contact number and names

Bacteria
  • 8,406
  • 10
  • 50
  • 67
mounika
  • 27
  • 10

3 Answers3

2

create two lists

 ArrayList<String> contacts_name = new ArrayList<String>();
 ArrayList<String> contacts_number = new ArrayList<String>();

        Cursor c = getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                null, null, null);
        while (c.moveToNext()) {

            contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            PhoneNumber = c
                    .getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            contacts_name.add("ContactName:"+contactName);
            contacts_number.add("ContactNumber:"+PhoneNumber );

        }

        c.close();
raj
  • 2,088
  • 14
  • 23
1

Create a class like this

public class NameContact {

private String name;
private String contact;

public NameContact(String name, String contact) {
    this.name = name;
    this.contact = contact;
}

public String getName() {
    return name;
}

public String getContact() {
    return contact;
}
}

then make an arraylist of it and add items in it..

ArrayList<NameContact> abc = new ArrayList<NameContact>();
abc.add(new NameContact("your_name","your_contact"));

Then After to put in RequestParams use this

RequestParams params = new RequestParams();
JSONArray arrayName = new JSONArray();
JSONArray arrayContact = new JSONArray();

for(i = 0;i < contacts.size();i++ ) {
    arrayName.put(contacts.get(i).getName());
    arrayContact.put(contacts.get(i).getContact());    
}
params.put("ContactNameArray",arrayName);
params.put("ContactNumberArray",arrayContact);
Mahesh B ツ
  • 128
  • 4
  • 14
0

You should go with pojo class or at the moment u can use this:

Arraylist<HashMap<String,String>> arr_list=new Arraylist<HashMap<String,String>>();

  arr_list.clear();

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

while (c.moveToNext()) {

        contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        PhoneNumber = c
                .getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

       HashMap<String,String> map=new HashMap<String,String>();
   map.put("name",""+contactName);
   map.put("number",""+PhoneNumber);    
   arr_list.add(map);   
    }

    c.close();

Log.d("array","arr="+arr_list);

you will have two keys and values with the position of array u can get the separate values.

Asif Sb
  • 785
  • 9
  • 38