Solid advice! We had a group of interns from a company come in and share there experience with our class, a lot of what you said resonated with what they had to say. Also a lot of my other questions I have not put here were answered. Sooo, i feel pretty prepared for next tomorrow.
Any advice on this:
*Notes
-
topic is my topic class --
DOES NOT FOLLOW JAVA NAMING CONVENTION IN THIS (gotta review how to rename correctly in Android Studio before I try to change again)
-topic class has instance arrays of questions / hint / answers.... and getters and setters for them also.
- not all my code is included below, but in here I believe lies the problem.
- I have another working version of this code, but super inefficient. (Creating an array of questions per topic etc.)
- Trying to follow teachers advice and be more generic / think more reusable.
- The app is a "flash" card type game, that has Questions Answers and Hints.
- In this code I'm trying to create an array of Topic objects which index is = the amount of topics in the spinner widget. I would like to populate the topic object with an array of Q/A/H...
- My approach was to make a arrayList that holds String[] elements and to add the xml string array resource into each index spot.
The String[] element of ArrayList never has a defined length, but it varies from topic --> topic how many questions it might have... Here lies my problem?!?-So in my for loop - I create an Topic object and I setQ/A/H_Array to the String[] within the ArrayList.
To me this should all work -- but I get
error code block after my codeCode
topic mTopics[];
List<String[]> mQuestions;
List<String[]> mAnswers;
List<String[]> mHints;
/////////////////////////////////////////////////////////////////////////////
mSpinnerCount = mSpinnerTopic.getCount(); // this happens after mSpinnerTopic widget is defined
mQuestions = new ArrayList<>();
mQuestions.add(getResources().getStringArray(R.array.countries_Q_Array));
mQuestions.add(getResources().getStringArray(R.array.presidents_Q_Array));
mQuestions.add(getResources().getStringArray(R.array.states_Q_Array));
mAnswers = new ArrayList<>();
mAnswers.add(getResources().getStringArray(R.array.countries_A_Array));
mAnswers.add(getResources().getStringArray(R.array.presidents_A_Array));
mAnswers.add(getResources().getStringArray(R.array.states_A_Array));
mHints = new ArrayList<>();
mHints.add(getResources().getStringArray(R.array.countries_H_Array));
mHints.add(getResources().getStringArray(R.array.presidents_H_Array));
mHints.add(getResources().getStringArray(R.array.states_H_Array));
mTopics = new topic[mSpinnerCount];
for (int i = 0; i<mSpinnerCount; i++) {
mTopics[i] = new topic();
mTopics[i].setQuestion_Array(mQuestions.get(i));
mTopics[i].setAnswer_Array(mAnswers.get(i));
mTopics[i].setHint_Array(mHints.get(i));
}
Code
2346-2346/com.bciancio.bciancioproject1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.bciancio.bciancioproject1, PID: 2346
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.bciancio.bciancioproject1.MainActivity$6.onItemSelected(MainActivity.java:170)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
at android.widget.AdapterView.access$200(AdapterView.java:48)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
I probably will hand in my inefficient version of this code since time is running out, buuuttt I'd still like to know how to do it at least more efficiently.
I know I didn't post a lot of the code, if you guys want more and or all of it, do let me know. Also if you have questions or need clarification on the functionality of this, I will try to relay it!
Thanks for your time!