简单的游标适配器和列表视图 [英] Simple Cursor Adapter and List View
问题描述
斜面生成的ListView随着SimpleCursorAdapter
这是数据处理程序我的光标方法:
Cant Generate ListView With SimpleCursorAdapter this is my Cursor Method in Data Handler:
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;
}
这是我的活动列表填充方法:
and this is my list populating method in 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();
}
在运行它模拟器/设备说:不幸的是停止,这是从日志猫错误:
after run it saying in emulator/device "Unfortunately stopped" and This are from log cat errors:
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)
有关详细资料:混帐
推荐答案
线索在你的日志猫错误的顶部:列'_id'不存在。请参见的Android列'_id'不存在?的解决方案。基本上,CursorAdapter的要求设置工作,所以只需将其添加到列列表中的结果列_id。如果你的表没有_id,要么添加它(从长远来看更容易),或使用rawQuery来选择你的数据,混淆ROWID作为_id。
The clue is at the top of your log cat errors: column '_id' does not exist. See Android column '_id' does not exist? for the solution. Basically, CursorAdapter requires the column _id in the results set to work so just add it to the list of columns. If your table does not have _id, either add it (easier in the long run) or use rawQuery to select your data, aliasing rowid as _id.
这篇关于简单的游标适配器和列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!