mercredi 6 mai 2015

Split out array list into separate strings

I have created an array of values from my SQLite database - this comprises of just two values from two different columns from an inner joined table.

in my SQLDatabaseHelper.java:

public List<List<String>> getAllAnswersByQuestion1() {
    List<String> array1 = new ArrayList<String>();  
    List<String> array2 = new ArrayList<String>();  

    SQLiteDatabase db = this.getReadableDatabase();

    String selectQuery = "SELECT  * FROM " + TABLE_ANSWERS + " ta, "
            + TABLE_QUESTION + " tq, " + TABLE_QUESTANS + " tqa WHERE  ta." + ASID
            + " = " + "tqa." + ASID + " AND tq." + QID + " = "
            + "tqa." + QID;

   Cursor c = db.rawQuery(selectQuery, null);
   if (c.moveToFirst()) {
    do {

     String questdescr = c.getString(c.getColumnIndex(QDESCR));
     String questid = c.getString(c.getColumnIndex(QID));
     array1.add(questdescr);
     array2.add(questid);

    } while (c.moveToNext());

 }
   List< List<String> > listArray = new ArrayList< List<String> >();
   listArray.add(array1);
   listArray.add(array2);

   return listArray;

}

I want to pass these to my main activity. Specifically I would like to split out the array into separate strings so I can assign those values to separate edittext fields.

This is my current attempt in my main activity:

    public void showNextRandomQuestion()
    {
    SQLDatabaseHelper db = new SQLDatabaseHelper(this);

    StringTokenizer tokens = new StringTokenizer(db.getAllAnswersByQuestion1(), ",");
    String first = tokens.nextToken();
    String second = tokens.nextToken();

    questionView.setText(first);
    answerText1.setText(second);
    }

However the code will not compile as it wants to either 'change the return type of 'getAllAnswersByQuestion1() to string' - or 'Remove argument to match StingTokenizer(String)'

Can't seem to get around this.

Aucun commentaire:

Enregistrer un commentaire