mounika
mounika

Reputation: 27

how to get the separate contact name array list and contact number array list from the device

   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

Upvotes: 1

Views: 1521

Answers (3)

Mahesh B ツ
Mahesh B ツ

Reputation: 128

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);

Upvotes: 1

Asif Sb
Asif Sb

Reputation: 805

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.

Upvotes: 0

raj
raj

Reputation: 2088

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();

Upvotes: 2

Related Questions