获取android联系电话号码列表

时间:2013-05-24 07:45:41

标签: android

我是android的新手。当我试图让联系人名称工作正常但我想只获取数字,但我无法这样做。我的代码是: -

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:Invalid column data1”。我已经google了很多并应用了一些解决方案但失败了。请帮帮我

4 个答案:

答案 0 :(得分:30)

    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
    }
      }
快照

enter image description here

确保您在清单中有此内容

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

答案 1 :(得分:8)

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

    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" />

我希望我能帮到你!

答案 2 :(得分:3)

@ Arpit:

在清单中添加权限。

但最重要的是...... 您是否注意到在格式化phoneNumber的行下面没有添加名称?您应该按照添加phoneNumber的方式将名称添加到数字中(我在下面的代码中附上):

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

上面代码的输出将是listview上的单独名称和数字(如下所示):


名称

名称

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

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

,输出将如下:


名 编号


名 编号


名 编号


祝你好运!

答案 3 :(得分:0)

找到下面的解决方案,它可用于从联系人列表中获取联系号。

您需要获得以下许可:

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

然后,调用联系人选择器:

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