17

我是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:无效列 data1”。我用谷歌搜索了很多并应用了一些解决方案但失败了。请帮帮我

4

4 回答 4

32
    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"/>
于 2013-05-24T07:52:48.220 回答
9

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

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

我希望我对你有所帮助!

于 2013-05-24T07:54:27.937 回答
3

.@Arpit:

在清单中添加权限。

但最重要的是...您是否注意到在您格式化 phoneNumber 的行下方没有添加姓名?您应该像添加电话号码一样将名称添加到号码中(我包含在下面的代码中):

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

上述代码的输出将是列表视图上的单独名称和数字(如下所示):


姓名

数字

姓名

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

aa.add(姓名+"\n"+电话号码);

输出将是这样的:


姓名号码


姓名号码


姓名号码


祝你好运!

于 2013-06-04T22:24:38.793 回答
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;
  }
 }
于 2014-02-27T10:56:44.297 回答