我在 Sqlite 数据库中有两个表。第一个表的列是_id
, name
。第二个表的列是_cid
, Category
。
我正在将第二个表中的数据检索到ListView
using 中SimpleCursorAdapter
。在日志中,我看到 aRuntimeException
和java.IllegalArgumentException: column '_id' does not exist
。
活动:
public class ActivityClass extends ListActivity{
private CursorAdapter curCateAdapter;
private ListView cateListview;
SimpleCursorAdapter adapter;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
cateListview=getListView();
final DatabaseConnector database = new DatabaseConnector(NoteCategoryActivity.this);
Cursor myCursor=database.getAllCategory();
String[] from = new String[] {"Category"};
int[] to = new int[] { R.id.cateTextView };
adapter= new SimpleCursorAdapter(this, R.layout.category_list,
myCursor,
from, to);
cateListview.setAdapter(adapter);
}
}
DatabaseHelper
班级:
public class DatabaseHelper {
private static final String DB_NAME = "NameDB.db";
private static final String TABLE_NAME1 = "table1";
private static final String TABLE_NAME2 = "Categories";
private static final String KEY_ID="_id";
private static final String KEY_NAME="Name";
public static final String KEY_NCID="_cid";
public static final String KEY_CAT="Category";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;
public Cursor getAllCategory() {
database=dbOpenHelper.getWritableDatabase();
Cursor mCursor =database.query("Categories", new String[]{"_cid","Category"}, null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
在category_list.xml
中,我定义了RelativeLayout
这个文件中使用的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cateTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center_vertical"
/>
</RelativeLayout>
logcat
输出:
02-26 15:17:35.729: E/AndroidRuntime(17757): FATAL EXCEPTION: main
02-26 15:17:35.729: E/AndroidRuntime(17757): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename.MyActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.os.Looper.loop(Looper.java:123)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 15:17:35.729: E/AndroidRuntime(17757):at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:17:35.729: E/AndroidRuntime(17757):at java.lang.reflect.Method.invoke(Method.java:507)
02-26 15:17:35.729: E/AndroidRuntime(17757):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 15:17:35.729: E/AndroidRuntime(17757): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 15:17:35.729: E/AndroidRuntime(17757): at dalvik.system.NativeStart.main(Native Method)
02-26 15:17:35.729: E/AndroidRuntime(17757): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.widget.CursorAdapter.init(CursorAdapter.java:111)
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
02-26 15:17:35.729: E/AndroidRuntime(17757): at intuition.itlabs.app.makenotes.MyActivity.onCreate(NoteCategoryActivity.java:39)
02-26 15:17:35.729: E/AndroidRuntime(17757): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)