How to get data from different SQLite tables and display it in one SimpleAdapter Android-ThrowExceptions

Exception or error:

I am new to Android and need to display Starttime, Stoptime and Name in a Listview. For displaying I use a SimpleAdapter. The problem is that I have two SQLite tables:

Table time:
Starttime | Stoptime | Student ID

Table student:
Student ID | Student name

Right now, I use a Cursor to get Starttime, Stoptime and Student ID from my database

 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);
 }

and then a SimpleAdapter to display it:

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});

But instead of the id, I want to display the names, which are stored in the other table. I can get the matching names of the Ids with another cursor

Cursor curm2 = myDb.getNamesbyIDs(arrayListStudentsName);
while (curm2.moveToNext()) {
        final HashMap<String, String> resultMapNames = new HashMap<>();
        resultMapNames.put("Name", curm2.getString(1));
}

But I don’t know how to get the names in the same adapter to display the names in the same list Item as the matching start & stop time.

EDIT

  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;
}
How to solve:

Change getAllMonday() to use a query that joins the 2 tables:

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;
}

Change the table variable TABLE_STUDENT and the column names that I used to the actual ones.
Now the cursor contains the name of the student and not the id.
Next change the code to:

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();

Leave a Reply

Your email address will not be published. Required fields are marked *