2

我正在尝试将联系人光标分配给适配器。我无法访问它。

它抛出以下错误。无法启动活动 java.lang.IllegalArgumentException:列 'data1' 不存在

在行
*dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_xml, cCursor, columns, to);*

下面是联系人的列表视图是我试图通过适配器实现的。

---------------------------------------
Contact_name_1         
Phone_no
---------------------------------------
Contact_name_2         
Phone_no2
---------------------------------------
Contact_name_3         
Phone_no3
---------------------------------------

代码如下。

public class NewContactTest1 extends Activity{

    ArrayAdapter<String> adapter;
    private SimpleCursorAdapter dataAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    Cursor cCursor = getAllCont();

    String[] columns = new String[]{

                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER
               };

        int[] to = new int[] { 
                R.id.name,
                R.id.phnumber
        };

    dataAdapter = new SimpleCursorAdapter(this, R.layout.contact_xml, cCursor, columns, to);   // I am getting error in here. 

    ListView listView = (ListView) findViewById(R.id.list_view);
    listView.setAdapter(dataAdapter);
    }

    public Cursor getAllCont()
    {
        Cursor cursor =
                getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                        null, null, null, null);
         if (cursor != null) 
         {  
             cursor.moveToFirst();
         }
         return cursor;
    }

它抛出以下错误。

联系人xml.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FFF"
    android:orientation="horizontal"
    android:padding="5dip"
>

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:visibility="invisible"
    />
    <TextView 
        android:id="@+id/phnumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sent_to"
        android:textColor="#333333"
        android:typeface="serif"
        android:textSize="15dip"
        android:background="#FFF"
    />

</RelativeLayout>

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

  <ListView 
    android:id="@+id/list_view"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 

  ></ListView>

</LinearLayout>
4

2 回答 2

1

访问联系人可能有点棘手,但幸运的是,这已经在 StackOverflow 上解决了:

最佳答案似乎回答了您的问题。

于 2012-11-29T03:50:04.727 回答
0

正如你所说的那样,我正在获取所有联系人

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

        ContactBean objContact = new ContactBean();
        objContact.setName(name);
        objContact.setPhoneNo(phoneNumber);
        list.add(objContact); 
       //here you get all contacts for cursor and display listview

    }
    phones.close();

    ContanctAdapter objAdapter = new ContanctAdapter(ContactListActivity.this, R.layout.alluser_row, list);
    listView.setAdapter(objAdapter);
于 2012-11-29T04:14:59.127 回答