我已经用谷歌搜索并搜索了这个网站的答案,但什么也没得到;/所以,这是我的问题,我有 listview,我尝试用数据库中的数据填充它,但列表什么也没显示。我真的坚持这个,这是我的代码:
DatabaseViewerActivity.java:
protected void onCreate(Bundle savedInstaceState)
{
super.onCreate(savedInstaceState);
setContentView(R.layout.database_layout);
SQLiteDatabase database = openOrCreateDatabase("qr_scanner.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
String q = "SELECT id as _id,serial,desc FROM inv_urzadzenia;";
Cursor c = database.rawQuery(q, null);
startManagingCursor(c);
ListAdapter adapter = new SimpleCursorAdapter(this,R.layout.single_row_item,c,new String[]{"serial","desc"},new int[]{R.id.deviceSerial,R.id.deviceDesc});
ListView lv = (ListView)findViewById(R.id.deviceList);
lv.setAdapter(adapter);
lv.setVisibility(View.VISIBLE);
c.close();
database.close();
}
这是我的 single_row_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayoutHeader"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView
android:id="@+id/deviceSerial"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight" android:layout_alignParentLeft="true"></TextView>
<TextView
android:id="@+id/deviceDesc"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight" android:layout_alignParentRight="true"></TextView>
</RelativeLayout>
和 database_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11pt"
android:text="Urzadzenia"/>
<RelativeLayout
android:id="@+id/RelativeLayoutHeader"
android:layout_height="wrap_content" android:layout_width="fill_parent">
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="String" android:layout_alignParentLeft="true" ></TextView>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textColor="#f00"
android:text="String" android:layout_alignParentRight="true"></TextView>
</RelativeLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deviceList" android:divider="#000" android:isScrollContainer="true" android:headerDividersEnabled="true" android:scrollbarAlwaysDrawVerticalTrack="true" android:scrollbars="vertical"/>
</LinearLayout>
我不知道问题出在哪里,我检查了 c.getCount() 和 adapter.getCount() 不返回 0,
我的日志猫:
09-20 07:23:43.574: W/KeyCharacterMap(8915): No keyboard for id 0 09-20 07:23:43.574: W/KeyCharacterMap(8915): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-20 07:28:37.994: W/ActivityManager(67): Activity pause timeout for HistoryRecord{43fd4b50 com.android.launcher/com.android.launcher2.Launcher}
当我从 statemet 中删除“AS _id”时出现的异常
09-20 07:32:36.644: E/AndroidRuntime(16725): java.lang.RuntimeException: Unable to start activity ComponentInfo{projects.qrscanner/projects.qrscanner.DatabaseViewerActivity}: java.lang.IllegalArgumentException: column '_id' does not exist