I have an Arraylist with two arrays in it. Depending on something from a different class, I want to either use the information from Array 1 or 2. However, I can't figure out how to refer indirectly to the specific array in the getQuestions method. I am able to receive the correct variable mQuestions1
or mQuestions2
in this class, in String format, and call this mVersion
.
My code:
public class QuestionLab {
ArrayList<Question> mQuestions1, mQuestions2;
private static QuestionLab sQuestionLab;
private Context mAppContext;
private QuestionLab(Context appContext) {
mAppContext = appContext;
mQuestions1 = new ArrayList<Question>();
mQuestions1.add(new Question(1, R.drawable.a, state));
mQuestions1.add(new Question(2, R.drawable.b, state));
mQuestions1.add(new Question(3, R.drawable.c, state));
mQuestions2 = new ArrayList<Question>();
mQuestions2.add(new Question(1, R.drawable.a, state));
mQuestions2.add(new Question(2, R.drawable.b, state));
mQuestions2.add(new Question(3, R.drawable.c, state));
}
public static QuestionLab get(Context c) {
if (sQuestionLab == null) {sQuestionLab = new QuestionLab(c.getApplicationContext());}
return sQuestionLab;
}
public Question getQuestion(int nr) {
for (Question c : mQuestions) {if (c.getNr() == (nr)) return c;} return null;
}
public ArrayList<Question> getQuestions(){return mQuestions1}
Writing mQuestions1
does work, but doesn't allow me to change between 1 & 2, so I want something like Arraylist(mVersion)
Thanks in advance!
it's rather easy.
Build a HashMap or another Basic Class which hold several childs.
Do you want to randomize it which question should be choosen? Why dont you create a helperClass which is called (for example Question)
which keep an array(list)?
class Question {
Arraylist<String> questions;
//;getter and setter
}
public ArrayList<Question> getQuestions(){
for (Question c : mQuestions) {
ArrayList<String> dummyArray = c.getQuestions();
if (c.size() > 0) {
//randomize it, get it out, whateer.
}
}
}
But getting back to your Question (and a Solution which is not a good technique doing this).
public ArrayList>Questions> getQuestions() {
if (question_array_1 != null && question_array_2 != null) {
int qCnt = (questions_array_1.size() > question_array_2.size()) ? question_array_1.size() : question_array_2.size();
for (int i = 0; <= qCnt; i++) {
String myQuestion = (Math.random(0,1) == 1) ? question_array_2.getQuestion() : question_array_1.getQuestion();
//or merge it to another arraylist...
}
}
}
Another Technique would be storing it directly into a SQLITEDatabase (which i pfefer!).
A last but not not least (and maybe not a good technique in your case) is adding all into one with
ArrayList<Question> array_list_all = new ArrayList<Question>();
array_list_all.addAll(first_question_array);
array_list_all.addAll(second_question_array);
Then you can foreach it and get the Question which triggers your level or questionID.
...
public static final int USE_FIRST = 0;
public static final int USE_SECOND = 1;
...
public Question getQuestion(final int mode, final int nr) {
ArrayList<Question> listToUse = null;
if (mode == USE_FIRST) {
listToUse = mQuestions1;
} else if (mode == USE_SECOND) {
listToUse = mQuestions2;
}
for (Question q : listToUse) ...
}
If you have more than two you can use Map
to store lists of questions:
Map<Integer, ArrayList<Question>> questionMap = new HashMap<Integer, ArrayList<Question>>();
questionMap.put(1, mQuestions1);
questionMap.put(2, mQuestions2);
questionMap.put(3, mQuestions3);
1 -> ArrayList #1
2 -> ArrayList #2
3 -> ArrayList #3
Hope I got your question right
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.