如何将 android 个联系人访问到数组中
How to access android contacts into an array
我刚刚开始 Android 和 Java 一般情况,我正在尝试编写一个应用程序,允许您 select 您的一些联系人,获取他们的姓名和主号码并将它们放入单独的列表数组中。我不确定该怎么做,所以我只是手动添加它,您必须在其中输入每个人的姓名和号码。
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
// Defines the box where we will grab the numbers
EditText addnumberfieldbox = (EditText) findViewById(R.id.addnumberfield);
EditText nameboxtext = (EditText) findViewById(R.id.namebox);
// Grabs the box's contents and puts it into useable data
Editable numberfieldEditable = addnumberfieldbox.getText();
Editable nameboxEditable = nameboxtext.getText();
// Finally uses the data from the box, and puts it in
String numberfieldString = numberfieldEditable.toString();
String nameboxString = nameboxEditable.toString();
names.add(nameboxString);
phnnumbers.add(numberfieldString);
这就是我用来获取文本并将其放入数组的方法。
1 - 将权限放在 AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<application
android:theme="@style/AppTheme" > ...
2 - 导入联系人合约
import android.provider.ContactsContract;
3 - 使用 phone 个联系人填充游标:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
4 - 填充数组列表
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
names.add(name);
phnnumbers.add(phoneNumber);
}
}
要访问您 phone 的联系人列表,您需要执行以下步骤:
第 1 步:添加权限以访问您的 manifest.xml 文件中的联系人
<uses-permission android:name="android.permission.READ_CONTACTS"/>
第 2 步: 使用以下代码获取姓名和 phone 号码,然后将它们存储到 ArrayList。
ArrayList<JSONObject> contacts = new ArrayList<>();
public void addContacts(){
//to store name-number pair
JSONObject obj = new JSONObject();
try {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
obj.put(name, phoneNumber);
contacts.add(obj);
Log.e("Contact list with name & numbers", " "+contacts);
}
phones.close();
}
catch (Exception e){
e.printStackTrace();
}
}
您可以在您的应用程序代码中使用 ArrayList 联系人。
我刚刚开始 Android 和 Java 一般情况,我正在尝试编写一个应用程序,允许您 select 您的一些联系人,获取他们的姓名和主号码并将它们放入单独的列表数组中。我不确定该怎么做,所以我只是手动添加它,您必须在其中输入每个人的姓名和号码。
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
TextView quantityTextView = (TextView) findViewById(R.id.numbersview);
// Defines the box where we will grab the numbers
EditText addnumberfieldbox = (EditText) findViewById(R.id.addnumberfield);
EditText nameboxtext = (EditText) findViewById(R.id.namebox);
// Grabs the box's contents and puts it into useable data
Editable numberfieldEditable = addnumberfieldbox.getText();
Editable nameboxEditable = nameboxtext.getText();
// Finally uses the data from the box, and puts it in
String numberfieldString = numberfieldEditable.toString();
String nameboxString = nameboxEditable.toString();
names.add(nameboxString);
phnnumbers.add(numberfieldString);
这就是我用来获取文本并将其放入数组的方法。
1 - 将权限放在 AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
<application
android:theme="@style/AppTheme" > ...
2 - 导入联系人合约
import android.provider.ContactsContract;
3 - 使用 phone 个联系人填充游标:
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
4 - 填充数组列表
List<String> phnnumbers = new ArrayList<String>();
List<String> names = new ArrayList<String>();
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
names.add(name);
phnnumbers.add(phoneNumber);
}
}
要访问您 phone 的联系人列表,您需要执行以下步骤:
第 1 步:添加权限以访问您的 manifest.xml 文件中的联系人
<uses-permission android:name="android.permission.READ_CONTACTS"/>
第 2 步: 使用以下代码获取姓名和 phone 号码,然后将它们存储到 ArrayList。
ArrayList<JSONObject> contacts = new ArrayList<>();
public void addContacts(){
//to store name-number pair
JSONObject obj = new JSONObject();
try {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
obj.put(name, phoneNumber);
contacts.add(obj);
Log.e("Contact list with name & numbers", " "+contacts);
}
phones.close();
}
catch (Exception e){
e.printStackTrace();
}
}
您可以在您的应用程序代码中使用 ArrayList 联系人。