listview适配器

时间:2012-02-29 10:51:13

标签: android listview

在datbase中我有三行,我已经检查过它们不是null ..

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**  android.app.Application@44f3f8c8
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的实例化存在一些问题...请帮帮我...

1 个答案:

答案 0 :(得分:0)

检查您的查询是否获得_id列(以及您显示的所有列)并将cursor.close();替换为startManagingCursor(cursor);

相关问题