obter list de numbers de telefone de contato do Android

Eu sou novo no android. Quando eu tento obter nomes de contato está funcionando bem, mas eu quero obter numbers apenas, mas não sou capaz de fazê-lo. Meu código é: –

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; @Oviewride 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[] airrayColumns = new String[]{ ContactsContract.CommonDataKinds.Phone.NUMBER}; // airrayViewID is the id of the view it will map to here textViewPhone only int[] airrayViewID = new int[]{R.id.textViewNumber}; Cursor cursor = getContentResolview().query(ContactsContract.Contacts.CONTENT_URI, airrayColumns, null, null, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact, cursor, airrayColumns, airrayViewID); listViewPhoneBook.setAdapter(adapter); }} 

Quando eu executo isso, ele diz "java.lang.IllegalArgumentException: dados de coluna inválidos1". Eu tenho googled muito e aplicado uma solução, mas falhou. Por favor, me ajude

4 Solutions collect form web for “obter list de numbers de telefone de contato do Android”

  getNumber(this.getContentResolview()); public void getNumber(ContentResolview 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); } } {  getNumber(this.getContentResolview()); public void getNumber(ContentResolview 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); } } {  getNumber(this.getContentResolview()); public void getNumber(ContentResolview 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); } } }  getNumber(this.getContentResolview()); public void getNumber(ContentResolview 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_pairent" android:layout_height="match_pairent" tools:context=".MainActivity" > <ListView android:layout_width="match_pairent" android:layout_height="fill_pairent" android:id="@+id/lv"/> </RelativeLayout> 

MainActivity.java

  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } }  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } {  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } {  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } }  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } }  public class MainActivity extends Activity { String phoneNumber; ListView lv; ArrayList <String> aa= new ArrayList<String>(); @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv= (ListView) findViewById(R.id.lv); getNumber(this.getContentResolview()); } public void getNumber(ContentResolview 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 } } 

tiro rápido

insira a descrição da imagem aqui

Certifique-se de que você tenha isso em manifesto

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

Minha solução paira recuperair todos os contatos:

  Cursor cursor = null; try { cursor = context.getContentResolview().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(); } } } catch (Exception e) {  Cursor cursor = null; try { cursor = context.getContentResolview().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(); } } se (cursor! = null) {  Cursor cursor = null; try { cursor = context.getContentResolview().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(); } } }  Cursor cursor = null; try { cursor = context.getContentResolview().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(); } } 

Você precisa dessa permissão em seu manifesto:

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

Espero ter ajudado você!

. @ Arpit:

Adicione a permissão em seu manifesto.

MAS MAIS IMPORTANTE … Você não percebeu que logo abaixo da linha onde você formatu o número de telefone que você não adicionou os nomes? Você deve adicionair o nome ao número da maneira como você adicionou o número de telefone (eu incluí no código abaixo):

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

a saída do código acima será nomes e numbers sepairados em sua listgem (como este):


nome

número

nome

Paira exibir isso em uma linha, você pode fazer isso:

aa.add (name + "\ n" + phoneNumber);

e a saída será assim:


número de nome


número de nome


número de nome


boa sorte!

Encontre a solução abaixo, funcionairá paira obter contato não da list de contatos.

Você precisa de permissão como:

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

Então, chamando o seletor de contatos:

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

Então,

  @Oviewride 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 Whateview you want to do with the selected contact name. } } break; } } }  @Oviewride 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 Whateview you want to do with the selected contact name. } } break; } } }  @Oviewride 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 Whateview you want to do with the selected contact name. } } break; } } }  @Oviewride 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 Whateview you want to do with the selected contact name. } } break; } } 
Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.