如何从设备获取单独的联系人姓名数组列表和联系人号码列表

时间:2015-12-03 11:13:15

标签: java android

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

在上面的代码中,获取姓名和电话号码但不分离联系电话和姓名的广告

3 个答案:

答案 0 :(得分:2)

创建两个列表

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

答案 1 :(得分:1)

创建一个这样的类

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

然后制作它的arraylist并在其中添加项目..

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

然后输入RequestParams后使用此

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

答案 2 :(得分:0)

你应该选择pojo课程,或者你现在可以使用它:

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( “阵列”, “ARR =” + arr_list);

你将拥有两个键和值,数组的位置可以得到单独的值。