我想用關係數據庫中的數據填充ListView。我試圖完成的是向NURSE_TABLE中的每位護士以及分配給該護士的房間號碼。我得到的結果是顯示兩個按鈕的佈局,以及一個空的ListView。空表ListView與多表SQLite數據庫
創建數據庫的代碼是:
public void onCreate(SQLiteDatabase db) {
// create NURSE_TABLE
db.execSQL("CREATE TABLE " + NURSE_TABLE + " (" + KEY_NURSE_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_LNAME
+ " TEXT NOT NULL, " + KEY_FNAME + " TEXT NOT NULL, "
+ KEY_MI + " TEXT, " + KEY_EMAIL + " TEXT NOT NULL, "
+ KEY_PHONE_PRIMARY + " TEXT NOT NULL, "
+ KEY_PHONE_SECONDARY + " TEXT, " + KEY_FTE
+ " TEXT NOT NULL, " + KEY_DUTY_STATUS + " TEXT NOT NULL);");
// create ROOM_TABLE
db.execSQL("CREATE TABLE " + ROOM_TABLE + " (" + KEY_ROOM_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TYPE_PATIENT
+ " TEXT, " + KEY_OCCUPIED + " TEXT NOT NULL, "
+ KEY_ROOM_NUMBER + " TEXT NOT NULL, "
+ KEY_ASSIGNED_NURSE + " INTEGER, "
+ " FOREIGN KEY (" + KEY_ASSIGNED_NURSE + ") REFERENCES "
+ NURSE_TABLE + " (" + KEY_NURSE_ROWID + "));");
} // onCreate
該活動的代碼是:
public void displayNurseRoster(){
ListView listView = (ListView) findViewById(R.id.list);
// int variables filled with NURSE_TABLE data
int[] to_nurseTable = new int[] {
// int list for data from NURSE_TABLE fields
R.id.rosterDutyStatus,
R.id.rosterListLname,
R.id.rosterListFname,
R.id.rosterListMI,
R.id.rosterListID,
// int list for data from ROOM_ROWID fields
R.id.firstAssignment,
R.id.secondAssignment,
R.id.thirdAssignment,
R.id.fourthAssignment,
R.id.fifthAssignment,
R.id.sixthAssignment };
// String array holding data fields from NURSE_TABLE
String[] from_nurseTable = new String [] {
// fields from NURSE_TABLE
DbCommunicator.KEY_DUTY_STATUS,
DbCommunicator.KEY_LNAME,
DbCommunicator.KEY_FNAME,
DbCommunicator.KEY_MI,
DbCommunicator.KEY_NURSE_ROWID,
DbCommunicator.KEY_ROOM_NUMBER};
// instantiate instance of DbCommunicator object
rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// get & manage cursor for NURSE_TABLE data
Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this);
startManagingCursor(nurseTableCursor);
// instantiate cursor adaptor
ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this,
R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable);
nurseTableCursor.moveToNext();
// set adapter to listView
listView.setAdapter(nurseTableAdapter);
}// displayNurseRoster()
生成遊標的代碼如下所示:
public Cursor getNurseRosterCursor (Context context) {
SQLiteQueryBuilder nurseQueryBuilder = new SQLiteQueryBuilder();
nurseQueryBuilder.setTables("NURSE_TABLE, ROOM_TABLE");
String[] returnedColumns = new String[] {
"Nurse_Table._id",
"Nurse_Table._dutyStatus",
"Nurse_Table._lName",
"Nurse_Table._FName",
"Nurse_Table._mi",
"Room_Table._id",
"Room_Table._roomNumber"};
Cursor nurseCursor = nurseQueryBuilder.query(sqldb, returnedColumns,
null, null, null, null, null);
return nurseCursor;
}
最後我的佈局看起來像這樣:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<Button
android:id="@+id/btnAddNurseToRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add"
android:focusable="true"/>
<Button
android:id="@+id/btnRemoveNurseFromRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnAddNurseToRoster"
android:layout_toRightOf="@+id/btnAddNurseToRoster"
android:text="@string/delete"
android:focusable="true"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/btnAddNurseToRoster"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
當我最初開始時,我只是用來自Nurses Table的數據填充ListView,它工作得很好,但自從我開始試圖讓兩張桌子一起工作以來,已經有了很長的一段時間。
任何幫助/方向將不勝感激。
問題是什麼? –
佈局出現在頂部的兩個按鈕,但列表視圖是完全空的。我在logcat上看到沒有錯誤 – deadEddie
請將此信息放入問題中。 –