2017-05-08 27 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