I’ve created a custom adapter for my spinner because I wanted to have multiple rows. However, I would basically like to recreate androids spinner model with the radio button that gets selected when a spinner item is selected. Everything is working fine except I don’t understand how to check the radio button when a list item is selected.
My SimpleCursorAdapter Custom Adapter code:
@Override
public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
super.newDropDownView(context, cursor, parent);
View view = View.inflate(context, R.layout.grain_spinner_row, null);
int nameColumn = cursor.getColumnIndex("name");
String getName = cursor.getString(nameColumn);
TextView name = (TextView)view.findViewById(R.id.GrainSpinnerName);
name.setText(getName);
int loviColumn = cursor.getColumnIndex("lovibond");
String getLovi = cursor.getString(loviColumn);
TextView lovi = (TextView)view.findViewById(R.id.GrainSpinnerLovibond);
lovi.setText(getLovi);
int gravityColumn = cursor.getColumnIndex("gravity");
String getGravity = cursor.getString(gravityColumn);
TextView gravity = (TextView)view.findViewById(R.id.GrainSpinnerGravity);
gravity.setText(getGravity);
rb = (RadioButton)view.findViewById(R.id.GrainSpinnerRadio);
return view;
}
public static void toggleRadio(){
if(!(rb.isChecked())){
rb.toggle();
}
}
OnSelectedItemChanged code:
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
//Collects the id from the currently selected item so we can use that to reference the ingredient in the recipe database.
//Because we are using a SimpleCursorAdapter to populate nameSpinner, we must use a cursor to request currentName.
Cursor cc = (Cursor)(nameSpinner.getSelectedItem());
currentName = cc.getString(cc.getColumnIndex("name"));
String sql = "SELECT * FROM grain WHERE name = '" + currentName + "' AND origin = '" + currentOrigin + "'";
Cursor data = database.rawQuery(sql, null);
data.moveToFirst();
int nameColumn = data.getColumnIndex("name");
int loviColumn = data.getColumnIndex("lovibond");
int gravityColumn = data.getColumnIndex("gravity");
int originColumn = data.getColumnIndex("origin");
String currentIngredientName = data.getString(nameColumn);
String currentIngredientLovi = data.getString(loviColumn);
String currentIngredientGravity = data.getString(gravityColumn);
String currentIngredientOrigin = data.getString(originColumn);
addIngredient = new String[4];
addIngredient[0] = currentIngredientName;
addIngredient[1] = currentIngredientLovi;
addIngredient[2] = currentIngredientGravity;
addIngredient[3] = currentIngredientOrigin;
GrainSpinnerAdapter.toggleRadio();
}
LogCat is giving me a nullexpression error at:
GrainSpinnerAdapter.toggleRadio();
and
if(!(rb.isChecked())){
If your question is as simple as it seems (merely changing the state of the radio button upon spinner selection) you can call the toggle() method on your radio button which will invert the radio button’s state. you can also declare your RadioButton and call an isChecked method.
EDIT:
then you can also have a method that reacts to the RadioButton’s check state using the isChecked() method. Good Luck!