如何将联系人姓名、号码、contact_id 保存到 ArrayList / 联系人选择器
Posted
技术标签:
【中文标题】如何将联系人姓名、号码、contact_id 保存到 ArrayList / 联系人选择器【英文标题】:how to save contacts names , numbers , contact_id to ArrayList / contact picker 【发布时间】:2014-06-29 05:19:40 【问题描述】:此活动正在运行,但我想将联系人信息(电话号码、姓名、联系人 ID)保存到 ArrayList 中。然后,我将使用 customadapter 在复选框中列出此数组列表以进行多选构造。我将在程序的后期使用联系信息。请帮助我你的想法。谢谢。
MainActivity.java
package com.javacodegeeks.android.androidphonecontactsexample;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.TextView;
public class MainActivity extends Activity
public TextView outputText;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outputText = (TextView) findViewById(R.id.textView1);
fetchContacts();
public void fetchContacts()
String phoneNumber = null;
String email = null;
//http://yazilimdevi.com/yazilimdevi/Makaleler-867-hashtable-vs--hashmap.aspx
//http://***.com/questions/19974166/contacts-adding-in-arraylist-for-listview-error
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
Uri EmailCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
String EmailCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
String DATA = ContactsContract.CommonDataKinds.Email.DATA;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null,null, null, null);
// Loop for every contact in the phone
if (cursor.getCount() > 0)
while (cursor.moveToNext())
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0)
output.append("\n First Name:" + name);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] contact_id , null);
while (phoneCursor.moveToNext())
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\n Phone number:" + phoneNumber);
phoneCursor.close();
// Query and loop for every email of the contact
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] contact_id , null);
while (emailCursor.moveToNext())
email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
output.append("\nEmail:" + email);
emailCursor.close();
output.append("\n");
outputText.setText(output);
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:textSize="20sp"
android:gravity="center"
android:text="@string/ContactsInformation" />
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignLeft="@+id/textView"
android:layout_alignRight="@+id/textView"
android:layout_below="@+id/textView"
android:gravity="center"
android:text="@string/TextView" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_
android:layout_
android:layout_above="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidPhoneContactsExample</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="ContactsInformation">Contacts Information</string>
<string name="TextView">TextView</string>
</resources>
【问题讨论】:
请求到站外资源的链接被认为是 Stack Overflow 的题外话。您尝试了什么,遇到了哪些具体问题? 对不起。我尝试了一些东西,但我删除了它们,因为有很多错误。之后我会再问一次,我在我的代码中发现了我的老问题。但我首先问了这个问题,也许有人可以从头开始指明方向。对不起我的英语:/我也看看这个链接,它与我的相似,但在那里找不到有用的答案:***.com/questions/12470363/… 我编辑了我的问题。现在,我需要使用 Arraylist 来保持联系。 【参考方案1】:在顶部添加一个数组列表
ArrayList<String> arr = new ArrayList<String>();
然后改变你的while循环如下
while (cursor.moveToNext())
output = new StringBuffer(); //re initialize your StringBuffer again
String contact_id = cursor.getString(cursor.getColumnIndex( _ID ));
String name = cursor.getString(cursor.getColumnIndex( DISPLAY_NAME ));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex( HAS_PHONE_NUMBER )));
if (hasPhoneNumber > 0)
output.append("\n First Name:" + name);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[] contact_id , null);
while (phoneCursor.moveToNext())
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
output.append("\n Phone number:" + phoneNumber);
phoneCursor.close();
// Query and loop for every email of the contact
Cursor emailCursor = contentResolver.query(EmailCONTENT_URI, null, EmailCONTACT_ID+ " = ?", new String[] contact_id , null);
while (emailCursor.moveToNext())
email = emailCursor.getString(emailCursor.getColumnIndex(DATA));
output.append("\nEmail:" + email);
emailCursor.close();
output.append("\n");
arr.add(output.toString()); //add contact here in your array list.
现在 arr 将包含所有联系人。
【讨论】:
非常感谢!另外我会问一个问题。这个 ArrayList 结构与“ ArrayList以上是关于如何将联系人姓名、号码、contact_id 保存到 ArrayList / 联系人选择器的主要内容,如果未能解决你的问题,请参考以下文章
如何在android studio中显示按地址排序的联系人姓名和号码