我为我的微调器创建了一个自定义适配器,因为我希望有多行。但是,我基本上想要使用在选择微调器项目时选择的单选按钮重新创建orroids微调器模型。一切都工作正常,但我不明白如何在选择列表项时检查单选按钮。
My SimpleCursorAdapter自定义适配器代码:
@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代码:
@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给我一个nullexpression错误:
GrainSpinnerAdapter.toggleRadio();
和
if(!(rb.isChecked())){
答案 0 :(得分:0)
如果您的问题看似简单(仅在微调器选择时更改单选按钮的状态),您可以在单选按钮上调用toggle()方法,这将反转单选按钮的状态。你也可以声明你的RadioButton并调用isChecked方法。
RadioButton myRadioButton = (RadioButton) findViewById(R.id.My_RadioButton);
public void YourSpinnerItemMethod () {
if ( !(myRadioButton.isChecked()) )
myRadioButton.toggle();
}
编辑:
OnSelectedItemChange {
YOUR_CODE;
Class_Containing_Radio_Button.toggleButton();
}
Class_Containing_Radio_Button.class {
RadioButton myButton;
toggleButton() {
myButton.toggle();
}
onCreate() {
myButton = (RadioButton) findViewById(R........);
}
}
然后您还可以使用isChecked()方法对RadioButton的检查状态做出反应。祝好运!
答案 1 :(得分:0)
我是这样做的......
首先,如果你使用的是SimpleCursorAdapter,你不需要提供一个可以扩展视图的完整实现(newView) - 它可以帮到你。如果您需要这样做,那么您应该从CursorAdapter派生。总之...
我不确定Android系统如何在适配器上没有选定项目位置的情况下管理其视图的检查状态,所以我显然错过了一些东西,但这对我有用。