2017-05-08 50 views
0

我有一個列表視圖,其中填充了來自我的數據庫的數據。我使用simpleCursorAdapter來顯示值。獲取SimpleCursorAdapter中的外鍵值

我有一個表,我可以加課:英語,法語......

在另一張表,我可以創建developped課(我加開始和結束,這天的日期,主題爲教訓)。我必須以FK的身份提供這個教訓。

當我添加一個課程時,在我的listView中,我想顯示每個示例:英語 - 閱讀,但它顯示1 - 閱讀。因爲1是我在第二張表中存儲的值。

如何將1改爲英文?

這裏是我的代碼:

Cursor cursor = dbhelper.getAllCours(); 
    String[] from = { "branche_cours", "designation" }; //here 'branche_cours' is the lesson i store as an INT, it's the FK so 
    int[] to = { R.id.text_branche_cours, R.id.text_designation }; 
    adapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor, from, to, 0); 
    lvCours.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 

我使用getAllCours()

public Cursor getAllCours() 
{ 

    //from here, i retrieve the ID, designation and branche_cours 
    String Query = ("select ID as _id, date_debut, date_dernier, dixieme_point, " + 
      "demi_point, description, designation, lundi, mardi, mercredi, jeudi," + 
      " vendredi, samedi, branche_cours from " + TABLE_COURS); 

    Open(); 

    Cursor cursor = db.rawQuery(Query, null); 

    return cursor; 
} 

我如何可以鏈接的int實際價值(所以如何能在 '1' 變成「英語的方法「)?

+0

我還沒有找到解決辦法.. – David

回答

2

一個解決辦法是執行SQL JOIN操作,從兩個表獲取數據:

那麼SQL查詢應該是東西一樣:

SELECT table1.branche_cours, table2.designation 
FROM table1 
INNER JOIN table2 ON table1.ID=table2.ID; 
+0

我測試就這樣,和它的工作,感謝ÿ ou求救! – David

1

要查找的值在另一個表,你可以使用一個correlated subquery

SELECT ID AS _id, 
     ..., 
     samedi, 
     (SELECT name 
     FROM other_table 
     WHERE other_table.id = cours.branche_cours 
     ) AS branche_cours 
FROM cours; 
+0

這也適用,謝謝! – David