2012-11-29 98 views
2

我想將一個聯繫人光標分配給適配器。我無法訪問它。無法通過聯繫人API(ContactsContract)訪問電話號碼列

它拋出錯誤。 無法啓動活動java.lang.IllegalArgumentException異常:列 'DATA1' 不存在

在管線
*的DataAdapter =新SimpleCursorAdapter(此,R.layout.contact_xml,cCursor,列,至); *

下面是我想通過適配器實現的聯繫人列表視圖。

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

它拋出錯誤。

contact_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> 

main.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> 

回答

0

這裏的IAM歌廳所有聯繫人按你說的做這樣的

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); 
相關問題