I'm confused about how to determine the array for my spinner based on the selected radio button.
So I have 1 spinner and 1 RadioButtonGroup contain with 2 RadioButtons. My case is like :
- If RadioButton_A is selected then the spinner will display this list :
<string-array name="spinner_item_a"> <item>plant 1</item> <item>plant 2</item> <item>plant 3</item> </string-array>
- If RadioButton_A is selected then the spinner will display this list :
<string-array name="spinner_item_b"> <item>fruit 1</item> <item>fruit 2</item> <item>fruit 3</item> </string-array>
I'm using SharedPreferences to pass the value of the RadioButton so that the spinner can get the value. Here's my code for selected RadioButton :
onSelectedChapterChange = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
chapters = group.getCheckedRadioButtonId() == R.id.chapter_1_radio_button ? plant : fruit;
if (chapters.equals(plant))
setPrefType = "Plant";
if (nationality.equals(fruit))
setPrefType = "Fruit";
SharedPreferences.Editor sPref = context.getSharedPreferences("ChapterTypePref", Context.MODE_PRIVATE).edit();
sPref.putString("ChapterInfo",setPrefType);
sPref.apply();
}
};
Here's my listener for spinner :
chapterSelectedListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position > 0) {
chapterType = chapterItemValueList.get(position-1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
Here's how I set the list of item into the adapter :
private void initChapterSpinner(final Context context, Resources resources) {
SharedPreferences pref = context.getSharedPreferences("ChapterTypePref", Context.MODE_PRIVATE);
String chapterPref = pref.getString("ChapterInfo","");
if (chapterPref == "Plant"){
chapterItemValueList = new ArrayList<>(Arrays.asList(
resources.getStringArray(R.array.spinner_item_a)));
} else if (chapterPref == "Fruit"){
chapterItemValueList = new ArrayList<>(Arrays.asList(
resources.getStringArray(R.array.spinner_item_b)));
}
idTypeArrayAdapter = new IdTypeSpinnerAdapter(context, idTypeItemValueList);
idTypeArrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_cdd);
}
It turns out that, the list item of the spinner adapter becomes null. I already got the logic on how's the flow should be run, but it's just confusing on implementing it. Please kindly help me, Thank you :D