我是Android新手,需要在列表视图中显示开始时间,停止时间和名称。为了显示,我使用一个SimpleAdapter。问题是我有两个SQLite表:
上桌时间:启动时间|停工时间|学生证
餐桌学生:学生证|学生姓名
现在,我使用Cursor从数据库中获取开始时间,停止时间和学生ID
Cursor curm1 = myDb.getAllMonday();
while (curm1.moveToNext()) {
final HashMap<String, String> resultMapMonday = new HashMap<>();
resultMapMonday.put("Start", curm1.getString(3));
resultMapMonday.put("Stop", curm1.getString(4));
resultMapMonday.put("Student", curm1.getString(5));
arrayListStudentsName.add(curm1.getString(5));
listItemsMo.add(resultMapMonday);
}
然后显示一个SimpleAdapter:
final SimpleAdapter adaptersimpleMo = new SimpleAdapter(this, listItemsMo, R.layout.timeslots_configurate,
new String[]{"Start", "Stop", "Student"},
new int[]{R.id.oneTime_start, R.id.oneTime_stop, R.id.selectedStudent});
但是我想显示名称,而不是ID,这些名称存储在另一个表中。我可以使用另一个光标来获取ID的匹配名称
Cursor curm2 = myDb.getNamesbyIDs(arrayListStudentsName);
while (curm2.moveToNext()) {
final HashMap<String, String> resultMapNames = new HashMap<>();
resultMapNames.put("Name", curm2.getString(1));
}
但是我不知道如何在相同的适配器中获取名称,以在与匹配的开始和结束时间相同的列表项中显示名称。
编辑
public Cursor getAllMonday() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_TIME + " WHERE day = 'Montag' ORDER BY CAST(start as unsigned)", null);
return res;
}
更改getAllMonday()
以使用连接两个表的查询:
public Cursor getAllMonday() {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "SELECT t.Start, t.Stop, s.Studentname FROM " + TABLE_TIME + " AS t INNER JOIN " + TABLE_STUDENT +
" AS s ON s.StudentID = t.StudentID WHERE t.day = 'Montag' ORDER BY CAST(t.start as unsigned)"
Cursor res = db.rawQuery(sql, null);
return res;
}
将表变量TABLE_STUDENT
和我以前使用的列名更改为实际名称。现在,光标包含学生的姓名,而不是ID。接下来将代码更改为:
Cursor curm1 = myDb.getAllMonday();
while (curm1.moveToNext()) {
final HashMap<String, String> resultMapMonday = new HashMap<>();
resultMapMonday.put("Start", curm1.getString(curm1.getColumnIndex("Start")));
resultMapMonday.put("Stop", curm1.getString(curm1.getColumnIndex("Stop")));
resultMapMonday.put("Student", curm1.getString(curm1.getColumnIndex("Studentname")));
arrayListStudentsName.add(curm1.getString(curm1.getColumnIndex("Studentname")));
listItemsMo.add(resultMapMonday);
}
curm1.close();