Reputation: 5027
Sorry for seem making so clumpsy for the below code. To sum up, I am making a quiz game where user can choose 4 kinds of difficulties of the Question. if user choose checkbox 1 and 3, for example, the below code is intended to store string arrays from the string.xml into String[] NUM_ALL_QuestionNames.
//same for checkbox1,2,3, so here not duplicate //
if (checkbox4=="yes")
{
NUM_EXP_QuestionNames = getResources().getStringArray(R.array.Num_Q_Expert_List);
NUM_EXP_AnswerNames = getResources().getStringArray(R.array.Num_A_Expert_List);
QuestionImport= 0;
QuestionImport = NUM_EXP_QuestionNames.length;
int i =0;
while (i<QuestionImport)
{
String Q_toimport = NUM_EXP_QuestionNames[i];
String A_toimport = NUM_EXP_AnswerNames[i];
NUM_ALL_QuestionNames.add(Q_toimport);
NUM_ALL_AnswerNames.add(A_toimport);
++i;
}
};
NUM_ALLL_QuestionNames = new String[NUM_ALL_QuestionNames.size()]; //convert ArrayList<String> to String[]
NUM_ALLL_AnswerNames = new String[NUM_ALL_AnswerNames.size()]; //convert ArrayList<String> to String[]
At last NUM_ALL_Questionnames would be converted back from ArrayList NUM_ALLL_QuestionNames to String[] for further processing.
The files in the string.xml should have no problem because when I try set as follows (directly extract from the string.xml) the apps runs fine:
NUM_ALLL_QuestionNames = getResources().getStringArray(R.array.Num_Q_Simple_List);
NUM_ALLL_AnswerNames = getResources().getStringArray(R.array.Num_A_Simple_List);
Now is it data from string.xml in String[] format, it was then added to the List, where this List at last convert back to String[]. Are there any modification such that is no need this conversion. It is better if string.xml can be directly to String[]
When goes on executeing the app, logcat does not show any error. However, the Emulator keep black screen. Why does it happen? Are there any endless loop for the Code?
Many thanks!
Upvotes: 0
Views: 490
Reputation: 3846
This is wrong:
NUM_ALLL_QuestionNames = new String[NUM_ALL_QuestionNames.size()]; //convert ArrayList<String> to String[]
It doesn't convert an ArrayList to a String[], it sets the value of your variable to an empty String[]. To convert, do this:
NUM_ALLL_QuestionNames = NUM_ALL_QuestionNames.toArray();
assuming that NUM_ALL_QuestionNames
is a List.
And the Doctor is right, that string comparison doesn't work in Java.
Upvotes: 2
Reputation: 17115
I don't see why the checkbox4 value is a string, but don't ever compare strings through == because this way you compare links that might point to different objects. Instead, use the equals method from String like this if (checkbox4.equals("yes"))
Upvotes: 0