2012-02-29 87 views
0

在datbase我有我檢查了,他們是不是空三排..的ListView適配器

SimpleCursorAdapter adapter; //class bvariable 
public void showlist() 
    { 
     Log.i("BookShelf", "11111111111"); 
     String[] col={DbHelper.BOOK_NAME ,DbHelper.FLAG}; 
     Cursor cursor=db.query(DbHelper.TABLE,col,null,null,null,null,null); 
     if((cursor !=null)&&(cursor.getCount()>0)) 
     { 
      Log.i("BookShelf", "**list view adapter 5** " + getApplicationContext()); 
     adapter = new ListViewAdapter(getApplicationContext(), cursor); 
     Log.i("BookShelf", "show list ke andar 5"); 
      listview.setAdapter(adapter); 
      Log.i("BookShelf", "show list ke andar 6"); 
     } 
       cursor.close(); 
       } 

package com.himanshu; 

import android.content.Context; 
import android.database.Cursor; 
import android.text.format.DateUtils; 
import android.util.Log; 
import android.view.View; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 

public class ListViewAdapter extends SimpleCursorAdapter 
{ 
    static String[] FROM={DbHelper.BOOK_NAME }; 
    static int[] TO ={R.id.book_name}; 

public ListViewAdapter(Context context, Cursor c) 
{ 
super(context, R.layout.row, c, FROM, TO); 
Log.i("BookShelf", "list view adapter"); 
} 
// This is where the actual binding of a cursor to view happens 
@Override 
public void bindView(View row, Context context, Cursor cursor) 
{ 
super.bindView(row, context, cursor); 
Log.i("BookShelf", " bind view "); 
int flag = cursor.getInt(cursor.getColumnIndex(DbHelper.FLAG)); 
if(flag==1) 
{ 
TextView tick = (TextView) row.findViewById(R.id.tick); 
tick.setText("ADDED"); 
} 
} 
} 

和日誌看起來像::::

02-29 16:01:20.862: I/BookShelf(2437): **list view adapter 5** [email protected] 
02-29 16:01:20.881: D/AndroidRuntime(2437): Shutting down VM 
02-29 16:01:20.881: W/dalvikvm(2437): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
02-29 16:01:20.921: E/AndroidRuntime(2437): FATAL EXCEPTION: main 
02-29 16:01:20.921: E/AndroidRuntime(2437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.himanshu/com.himanshu.AddActivity}: java.lang.IllegalArgumentException: column '_id' does not exist 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.os.Looper.loop(Looper.java:123) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at dalvik.system.NativeStart.main(Native Method) 
02-29 16:01:20.921: E/AndroidRuntime(2437): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.widget.CursorAdapter.init(CursorAdapter.java:111) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:90) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at com.himanshu.ListViewAdapter.<init>(ListViewAdapter.java:18) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at com.himanshu.AddActivity.showlist(AddActivity.java:200) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at com.himanshu.AddActivity.onCreate(AddActivity.java:91) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-29 16:01:20.921: E/AndroidRuntime(2437):  ... 11 more 

等看到日誌它明確表示,ListViewAdapter的實例化有一些問題...請幫我...

+0

的可能的複製http://stackoverflow.com/questions/5812030/java-lang-illegalargumentexception-column-id-does-not-exist – 2012-02-29 10:53:42

+2

確保,你的表和光標,你傳遞給適配器類,包含_id列 – Hiral 2012-02-29 10:54:37

+0

@Hiral :: thanx for ur的建議..我已經包含_id在遊標中,現在它的工作.. ListViewAdapter的構造函數被調用,但它沒有調用bindView函數,也沒有設置適配器也...日誌看起來像:: ----------- SHOW LIST KE ANDAR 5 [email protected] --------- list view adapter - -------顯示列表ke andar 5 -----------顯示列表kearar 6 – 2012-02-29 11:05:25

回答

0

檢查您的查詢獲取_id列(和顯示的所有列)並將cursor.close();startManagingCursor(cursor);