无法使用SimpleCursorAdapter生成ListView 这是我在数据处理程序中的Cursor方法:
public Cursor getAllRows()
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(true,TABLE_ITEM,new String[]{KEY_NAME,KEY_VALUE},null,null,null,null,null,null);
if(cursor != null)
{
cursor.moveToFirst();
}
return cursor;
}
这是我在Activity中的列表填充方法:
public void makeList()
{
Cursor cursor = db.getAllRows();
String[] fromField = new String[]{"item_name", "item_name"};
int[] toView = new int[]{R.id.tvName, R.id.tvVal};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,R.layout.content_layout,cursor,fromField,toView,0);
ListView lv = (ListView) findViewById(R.id.listView8);
lv.setAdapter(sca);
}
并在此处调用此方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status);
makeList();
}
运行之后,在模拟器/设备中说#34;不幸的是停止"这来自log cat错误:
09-04 14:55:48.455 32680-32680/com.alright.plastic.dailyex E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.alright.plastic.dailyex, PID: 32680
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alright.plastic.dailyex/com.alright.plastic.dailyex.StatusActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
at com.alright.plastic.dailyex.StatusActivity.makeList(StatusActivity.java:109)
at com.alright.plastic.dailyex.StatusActivity.onCreate(StatusActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
详细信息:git
答案 0 :(得分:0)
线索位于日志cat错误的顶部:列&#39; _id&#39;不存在。有关解决方案,请参阅Android column '_id' does not exist?。基本上,CursorAdapter要求结果集中的列_id工作,所以只需将其添加到列列表中即可。如果您的表没有_id,请添加它(从长远来看更容易)或使用rawQuery选择数据,将rowid别名为_id。