Android 列表视图适配器

Android 列表视图适配器,android,listview,Android,Listview,在datbase中,我有三行,我已检查它们是否为空 SimpleCursorAdapter adapter; //class bvariable public void showlist() { Log.i("BookShelf", "11111111111"); String[] col={DbHelper.BOOK_NAME ,DbHelper.FLAG}; Cursor cursor=db.query(DbHelper.TABLE,c

在datbase中,我有三行,我已检查它们是否为空

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
02-29 16:01:20.862:I/BookShelf(2437):**列表视图适配器5**android.app。Application@44f3f8c8
02-29 16:01:20.881:D/AndroidRuntime(2437):关闭虚拟机
02-29 16:01:20.881:W/dalvikvm(2437):threadid=1:线程以未捕获异常退出(组=0x4001d800)
02-29 16:01:20.921:E/AndroidRuntime(2437):致命异常:主
02-29 16:01:20.921:E/AndroidRuntime(2437):java.lang.RuntimeException:无法启动活动组件信息{com.himanshu/com.himanshu.AddActivity}:java.lang.IllegalArgumentException:列'\u id'不存在
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于android.os.Handler.dispatchMessage(Handler.java:99)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.os.Looper.loop(Looper.java:123)上
02-29 16:01:20.921:E/AndroidRuntime(2437):位于android.app.ActivityThread.main(ActivityThread.java:4627)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于java.lang.reflect.Method.Invokenactive(本机方法)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于java.lang.reflect.Method.invoke(Method.java:521)
02-29 16:01:20.921:E/AndroidRuntime(2437):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-29 16:01:20.921:E/AndroidRuntime(2437):在dalvik.system.NativeStart.main(本机方法)
02-29 16:01:20.921:E/AndroidRuntime(2437):原因:java.lang.IllegalArgumentException:列“\u id”不存在
02-29 16:01:20.921:E/AndroidRuntime(2437):位于android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于android.widget.CursorAdapter.init(CursorAdapter.java:111)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.widget.CursorAdapter.(CursorAdapter.java:90)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:47)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:84)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于com.himanshu.ListViewAdapter。(ListViewAdapter.java:18)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于com.himanshu.AddActivity.showlist(AddActivity.java:200)
02-29 16:01:20.921:E/AndroidRuntime(2437):位于com.himanshu.AddActivity.onCreate(AddActivity.java:91)
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)上
02-29 16:01:20.921:E/AndroidRuntime(2437):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-29 16:01:20.921:E/AndroidRuntime(2437):。。。还有11个

因此,在查看日志后,很明显ListViewAdapter的实例化存在一些问题。。。请帮我解决…

检查您的查询是否获得
\u id
列(以及您显示的所有列),并替换
游标。close()带有
开始管理光标(光标)

传递到适配器类的表和光标可能包含_idcolumn@Hiral::谢谢你的建议。。我已将_id包含在光标中,现在它正在工作。。正在调用ListViewAdapter的构造函数,但它没有调用bindView函数,也没有设置适配器。。。日志如下所示::------------显示列表KE ANDAR 5 android.app。Application@44f3f8c8------列表视图适配器------显示列表ke andar 5-------显示列表ke andar6@HimanshuKohli:为什么不尝试重写适配器的getView()方法?