2012-09-09 71 views
0

我想用關係數據庫中的數據填充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,它工作得很好,但自從我開始試圖讓兩張桌子一起工作以來,已經有了很長的一段時間。

任何幫助/方向將不勝感激。

+0

問題是什麼? –

+0

佈局出現在頂部的兩個按鈕,但列表視圖是完全空的。我在logcat上看到沒有錯誤 – deadEddie

+0

請將此信息放入問題中。 –

回答

0

您的setTables()調用中的兩個表未正確加入。

from_nurseTableto_nurseTable數組不匹配。

您還沒有提到要在列表視圖中顯示什麼,但我猜它可能是這樣的查詢結果:

SELECT nurse_table._id,_dutyStatus,_lName,_FName,_mi,group_concat(_roomNumber) 
    FROM nurse_table LEFT JOIN room_table ON nurse_table._id = _assignedNurse 
    GROUP BY nurse_table._id 
    ORDER BY _lName,_FName /* or whatever */ 

當你不知道如何爲了構建您的SQL查詢,在桌面上的數據庫副本中測試查詢通常會更有幫助。 (有許多SQLite管理工具,或者最後一次使用sqlite(.exe)命令行工具。)

+0

我試圖展示的東西是這樣的: – deadEddie

+0

關於約翰遜羅賓R(下一行)任務:房間1房間2 – deadEddie

+0

但我很苦惱如何顯示,因爲房間的數量可能會改變一護士到另一個。感謝您的迴應,我正在研究如何正確加入表格。 – deadEddie