获取android联系人电话号码列表

我是新来android.When我尝试获得联系人名称工作正常,但我只想获得数字,但我无法这样做。 我的代码是:

package com.example.sqllitecontactlist; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; public class PhoneBookActivity extends Activity { //Android listview object ListView listViewPhoneBook; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.phone_book); //get the ListView Reference from xml file listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook); String[] arrayColumns = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER}; // arrayViewID is the id of the view it will map to here textViewPhone only int[] arrayViewID = new int[]{R.id.textViewNumber}; Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, arrayColumns, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact, cursor, arrayColumns, arrayViewID); listViewPhoneBook.setAdapter(adapter); }} 

当我执行这个它说:“java.lang.IllegalArgumentException:列数据1无效”。 我GOOGLE了很多,并应用一些解决scheme,但失败了。 请帮我一下

Solutions Collecting From Web of "获取android联系人电话号码列表"

  getNumber(this.getContentResolver()); public void getNumber(ContentResolver cr) { Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); // use the cursor to access the contacts while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); // get display name phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // get phone number System.out.println(".................."+phoneNumber); } } 

activity_main.xml中

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:layout_width="match_parent" android:layout_height="fill_parent" android:id="@+id/lv"/> </RelativeLayout> 

MainActivity.java

  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolver()); } public void getNumber(ContentResolver cr) { Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); System.out.println(".................."+phoneNumber); aa.add(phoneNumber); } phones.close()// close cursor ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,aa); lv.setAdapter(adapter); //display contact numbers in the list } } 

快照

在这里输入图像说明

确保你有这个清单

  <uses-permission android:name="android.permission.READ_CONTACTS"/> 

我的解决scheme恢复所有联系人:

  Cursor cursor = null; try { cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); int contactIdIdx = cursor.getColumnIndex(Phone._ID); int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME); int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER); int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID); cursor.moveToFirst(); do { String idContact = cursor.getString(contactIdIdx); String name = cursor.getString(nameIdx); String phoneNumber = cursor.getString(phoneNumberIdx); //... } while (cursor.moveToNext()); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) { cursor.close(); } } 

您的清单中需要此权限:

 <uses-permission android:name="android.permission.READ_CONTACTS" /> 

我希望我已经帮助你了!

@ Arpit:

在清单中添加权限。

但最重要的是…你没有注意到,你刚刚在格式化phoneNumber线,你没有添加名称? 您应该添加名称到号码只是添加phoneNumber的方式(我在下面的代码中):

 ..System.out.println(".................."+phoneNumber); aa.add(name); aa.add(phoneNumber); 

上面的代码的输出将是你的列表视图上的独立的名字和数字(像这样):


名称

名称

为了在一行中显示,你可以这样做:

aa.add(名+ “\ n” + phoneNumber的);

和输出将是这样的:


名字号码


名字号码


名字号码


祝你好运!

find下面的解决scheme,它将用于从联系人列表中获取联系人号码。

您需要像下面的权限:

  android:name="android.permission.READ_CONTACTS"/> 

然后,调用联系人select器:

  Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 

然后,

  @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if (c.moveToFirst()) { String name = c.getString(c.getColumnIndexOrThrow(People.NAME)); // TODO Whatever you want to do with the selected contact name. } } break; } }