0

我試圖把姓名和電話號碼從通訊錄到我使用自定義的CursorAdapter的幫助下定製的ListView存在,但我收到以下錯誤獲取拋出:IllegalArgumentException:列「數據1」不存在的Android

異常

11-07 17:53:39.619: ERROR/AndroidRuntime(628): FATAL EXCEPTION: main 
11-07 17:53:39.619: ERROR/AndroidRuntime(628): java.lang.IllegalArgumentException: column  'data1' does not exist 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:99) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at org.com.apis.ContactListCustomCursorAdapter.bindView(ContactListCustomCursorAdapter.java:37) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.CursorAdapter.getView(CursorAdapter.java:186) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.AbsListView.obtainView(AbsListView.java:1315) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.ListView.onMeasure(ListView.java:1109) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.View.measure(View.java:8171) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.View.measure(View.java:8171) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.View.measure(View.java:8171) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.View.measure(View.java:8171) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.ViewRoot.performTraversals(ViewRoot.java:801) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.os.Looper.loop(Looper.java:123) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-07 17:53:39.619: ERROR/AndroidRuntime(628):  at dalvik.system.NativeStart.main(Native Method) 

這裏是我使用

主要活動代碼

守則3210
public class ManipulationActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.contacts_list_layout); 

    Cursor cursor = getCursor(); 


    ListView contactList = (ListView)findViewById(R.id.contactList); 
    contactList.setAdapter(new ContactListCustomCursorAdapter(this, cursor)); //ContactListCustomCursorAdapter is a customized CursorAdapter. I have put the code below this class 

} 

    //Get Cursor pointing Contacts 
private Cursor getCursor() { 

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    while (cursor.moveToNext()) { 
     String contactId = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts._ID)); 
     String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
     if (Boolean.parseBoolean(hasPhone)) { 
      // You know it has a number so now query it like this 
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
      while (phones.moveToNext()) { 
      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));     
      } 
     phones.close(); 
     } 
    } 
    cursor.close(); 
    return cursor; 
}  

} 

ContactListCustomCursorAdapter代碼

public class ContactListCustomCursorAdapter extends CursorAdapter { 

private Context context; 
private Cursor cursor; 
private LayoutInflater inflater; 
private int username; 
private int phoneNumber; 

public ContactListCustomCursorAdapter(Context context, Cursor c) { 
    super(context, c); 
    this.context = context; 
    this.cursor = c; 
    inflater = LayoutInflater.from(context); 

    username = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME); 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView userName = (TextView)view.findViewById(R.id.userName); 
    userName.setText(cursor.getString(username)); 

    TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber); 
    phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    final View view = inflater.inflate(R.layout.custom_contact_list_item, parent, false); 

    return view; 
} 

} 

我不想使用聯繫人選擇器。

回答

0

首先,您傳遞給CursorAdapter的遊標已關閉。如果您將光標分配給適配器,則不應將其關閉,只需在onDestroy中爲您的活動調用changeCursor(null)即可。

其次,從我假設你要顯示所有聯繫人和聯繫電話號碼的清單的代碼,所以你getCursor方法應該是這樣的

private Cursor getCursor() { 

    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 

    return cursor; 
} 

和你bindView應該像

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    TextView userName = (TextView)view.findViewById(R.id.userName); 
    userName.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))); 

    TextView phoneNumber = (TextView)view.findViewById(R.id.userNumber); 
    phoneNumber.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
} 
+0

我沒有使用任何光標適配器,也沒有訪問任何接觸或電話號碼...所以你答案不起作用... – Nik

+0

如果你不使用CursorAdapter,那麼你能解釋爲什麼你從CursorAdapter擴展嗎? – iago

0

我已經解決了這個問題,確保數據庫在需要時打開,並在沒有時關閉。

1

我也有這個錯誤,花了幾個小時後,我得到了答案。 還有CONSTANT COLUMN MISMATCH問題,'data1'或NUMBER是一個常數,屬於電話類。

ContactsContract。 聯繫人 .CONTENT_URI不提供數字,因爲您不得不提及使用查詢的投影,它會給出結果,在此幫助下您可以獲得._id,display_name。 但是,如果你想從身份證號碼,那麼你必須要這樣的事情,

Cursor cursorPhone = getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
       ContactsContract.Contacts._ID +" = " + item_ID, 
       null, 
       null); 

謝謝

相關問題