This is a follow on from a previous question I posted, so not sure if the discussions need to be merged. (Split out array list into separate strings)
I have two joined tables in my SQLite db for questions and answers. Every question has a set of three answers linked to it (and three pieces of additional text associated with each of those answers). From my database helper I am creating three arrays - one for all question strings from one column, one for all answer strings from all columns and one for all associated text strings from one column.
public List<List<String>> getAllAnswersByQuestion1() {
List<String> array1 = new ArrayList<String>();
List<String> array2 = new ArrayList<String>();
List<String> array3 = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_ANSWERS + " ta, "
+ TABLE_QUESTION + " tq WHERE ta." + ASID
+ " = " + "tq." + ASID;
Cursor c = db.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
String questdescr = c.getString(c.getColumnIndex(QDESCR));
String textdescr = c.getString(c.getColumnIndex(TDESCR));
String answersdescr = c.getString(c.getColumnIndex(ADESCR));
array1.add(questdescr);
array2.add(textdescr);
array3.add(answersdescr);
} while (c.moveToNext());
}
List< List<String> > listArray = new ArrayList< List<String> >();
listArray.add(array1);
listArray.add(array2);
listArray.add(array3);
return listArray;
}
In my main activity I am successfully picking a random question from the first array, and subsequently able to get one value of the answer text and associated text that are related to that particular question from the other two arrays.
However, what I'd really like to do is get the one random question from the first array, and then get all three associated text values from the second array and all three associated answer values from the third array - I would like to separate these out so I could assign these to different edittexts.
Here is my attempt which is looking at just splitting out the second array at the moment. But because I am using 'rand' it still seems to be only picking one of the values - so I can only split one string from it.
public void showNextRandomQuestion() {
SQLDatabaseHelper db = new SQLDatabaseHelper(this);
//get the data from the database
List<List<String>> listList = db.getAllAnswersByQuestion1();
//Get the question/answer Strings and the question IDs
List<String> questionStrings = listList.get(0); //question and answer Strings
List<String> textDescrs = listList.get(1); //text strings
List<String> answerDescrs = listList.get(2); //answer strings
//Generate random index
Random r = new Random();
int rand = Math.abs((r.nextInt() % questionStrings.size()));
//get answer description for randomly selected question
String questionString = questionStrings.get(rand);
String answerDescr = answerDescrs.get(rand);
// String textDescr = textDescrs.get(rand);
//Separate out the question/answer of text description associated with the randomly selected question
StringTokenizer textDescr = new StringTokenizer(textDescrs.get(rand), ",");
String first = textDescr.nextToken();
// String second = textDescr.nextToken();
// String third = textDescr.nextToken();
questionView.setText(questionString);
subQuestionView1.setText(first);
// subQuestionView2.setText(second);
// subQuestionView3.setText(third);
answerText1.setText(answerDescr);
}
Do I need to shuffle these two arrays based on 'rand'? Just not sure what that would look like as I haven't used it before. Thanks
Aucun commentaire:
Enregistrer un commentaire