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

6
  • Possible duplicate of Android Contact List Commented Dec 3, 2015 at 11:14
  • please briefly write the code Commented Dec 3, 2015 at 11:15
  • also contact name and number passed to post method to service Commented Dec 3, 2015 at 11:16
  • am adding the contacts_list.add(contactName ); contacts_list.add(PhoneNumber); but returning result should be name and phone number Commented Dec 3, 2015 at 11:18
  • make a custom pojo class instead, and by making arraylist of it you can achieve what you want Commented Dec 3, 2015 at 11:28

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

3 Comments

which value i should return i want to return both values in doInBackground
what you trying to do pls explain
By above code getting the name and phone number in two arraylists but which arraylist i returned both arraylists are returned possible for asynctask
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);

27 Comments

change the return type of your AsyncTask (doInBackground) to ArrayList<NameContact>.
did you got the solution or not.?
no one more passed array list of values to request parameters
one more getting array values that's fine and one more one these array values passed to request parameters
i cant get you what you want actually.?
|
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.

Comments

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.