I have two spinners in an activity, A and B. Each spinner uses an arrayAdapter linked to one or more string-arrays from an XML file. I need multiple because B’s values are populated depending on what is selected in A.
A reference to some arrays that B uses:
<!-- Used in spinner mCcColor -->
<string-array name="component0_color">
<item>Chocolate</item>
<item>Yellow</item>
<item>Strawberry</item>
</string-array>
<!-- Used in spinner mCcColor -->
<string-array name="component1_color">
<item>Blueberry</item>
<item>Chocolate</item>
<item>Dark Chocolate</item>
</string-array>
Next, I created a custom view class that is basically a canvas that I draw bitmaps (png) to. Spinner A is the bitmap, and spinner B is color. So, in order to determine what to draw in this view, I need to know what the user selected in spinner A and B.
It’s fairly easy to pass the position that was selected in both spinner A and spinner B. Here is an example.
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.cc_components, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCcComponent.setAdapter(adapter1);
mCcComponent.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Call the method of the custom view that draws to the canvas
mSelectedComponent = position;
myCustomView.foo(mSelectedComponent, mSelectedCompColor);
myCustomView.postInvalidate();
});
The problem I have is tying the index numbers of the spinners I pass into the custom view. I could do a bunch of switch case crap based on the position values I pass in, but this seems unnecessary because each case is just a few static values that correlate to the spinner. Plus then I would be tightly coupling my view class to the caller activity.
It seems like it would be much easier and cleaner to set-up a more complex array in XML and have it contain strings and 3 integer values for each string.
Is there a way I can use a complex array without causing headaches for my spinner, the image, or the colors inside the custom view? Is there a better way to approach this? Can anyone point me to some topics or links or post a few lines of code?
Thanks everyone!
Here is a visual representation of what I’m trying to explain, which seems like a cleaner way to solve this:
<!-- Used in spinner mCcColor -->
<array name="component1_color">
<item>Blueberry</item>
<red>81</red>
<green>27</green>
<blue>21</blue>
<item>Chocolate</item>
<red>45</red>
<green>21</green>
<blue>6</blue>
</array>
Why not create a class for each of these, so you don’t deal with the array directly, but go through functions?
Then you can have listeners if you want, so that the two spinners will be informed when there is a change and can adapt.
If you are worried about tight coupling, encapsulating the shared array will remove this difficulty.