2015-05-05 86 views
-1

我想顯示來自SQLite數據庫中的數據ListView。我使用CursorAdapter直接將Cursor插入ListView,但出於某種原因,我總是得到一個異常。 這是我的修改將對DBAdapter活動:如何創建一個列表視圖包含來自android中的sqlite數據

public static final String ID="_id"; 

    public Cursor feching_Data(){ 

      String[] columns = {ID,USER_NAME,USER_PASSWORD}; 
      db = dbHelper.getWritableDatabase(); 
      Cursor cursor = db.query(TABLE_NAME, columns,null,null,null,null,null); 
      return cursor; 

     } 

這裏我設置SimpleCursorAdapter

這是我收到的例外:

FATAL EXCEPTION: main 
    Process: com.developer.milanandroid, PID: 5109 
    java.lang.IllegalArgumentException: column '_id' does not exist 
    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) 
    at android.widget.CursorAdapter.init(CursorAdapter.java:172) 
    at android.widget.CursorAdapter.<init>(CursorAdapter.java:120) 
    at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52) 
    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78) 
    at com.developer.milanandroid.DatabaseListView$1.onClick(DatabaseListView.java:50) 
    at android.view.View.performClick(View.java:4438) 
    at android.view.View.onKeyUp(View.java:8241) 
    at android.widget.TextView.onKeyUp(TextView.java:5682) 
    at android.view.KeyEvent.dispatch(KeyEvent.java:2664) 
    at android.view.View.dispatchKeyEvent(View.java:7665) 
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) 
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) 
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) 
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) 
    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408) 
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035) 
    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505) 
    at android.app.Activity.dispatchKeyEvent(Activity.java:2418) 
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962) 
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3852) 
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826) 
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399) 
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449) 
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418) 
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525) 
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426) 
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582) 
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399) 
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449) 
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418) 
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426) 
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399) 
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449) 
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418) 
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3558) 
    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3718) 
    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2010) 
    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1704) 
    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1695) 
    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1987) 
    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141) 
    at android.os.MessageQueue.nativePollOnce(Native Method) 
    at android.os.MessageQueue.next(MessageQueue.java:138) 
    at android.os.Looper.loop(Looper.java:123) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 

回答

0

試想一下,在logcat的,錯誤的來源在第二行解釋:

java.lang.IllegalArgumentException: column '_id' does not exist. 

你的表沒有一個_id列,但CursorAdapter需要_id柱的存在(如文檔right here解釋)。


你的情況的解決方法很簡單:你已經有一個ID列,但它被稱爲ID。重命名爲_id,一切都應該正常工作!

請注意,您可能需要重新安裝應用以更改列和/或表格才能生效!

+0

我取代了我的ID,以_id,但它不工作dude.again發生相同的,請幫我哥們@Xaver Kapeller – dEePU

+0

你有沒有重新安裝該應用程序?而重新安裝,我的意思是完全從設備上卸載它,然後再部署它? –

+0

Nope dude我只是再次部署它,我只能通過卸載 – dEePU

相關問題