Android 自定义微调器:检查选择上的单选按钮
我为我的微调器创建了一个自定义适配器,因为我想要多行。但是,我基本上想使用选择微调器项目时选择的单选按钮重新创建 androids 微调器模型。一切工作正常,除了我不明白如何在选择列表项时检查单选按钮。
我的 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 在以下位置给我一个空表达式错误:
GrainSpinnerAdapter.toggleRadio();
和
if(!(rb.isChecked())){
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())){
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的问题像看起来一样简单(仅在选择微调器时更改单选按钮的状态),您可以在单选按钮上调用toggle() 方法,这将反转单选按钮的状态。您还可以声明 RadioButton 并调用 isChecked 方法。
编辑:
那么您还可以有一个使用 isChecked() 方法对 RadioButton 的检查状态做出反应的方法。祝你好运!
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!
我是这样做的......
首先,如果您使用 SimpleCursorAdapter,您不需要提供一个完整的实现(newView)来膨胀视图 - 它会为您做到这一点。如果您需要这样做,那么您应该从 CursorAdapter 派生。无论如何...
我不确定 Android 系统如何在适配器上没有选定项目位置的情况下管理其视图的检查状态,所以我显然错过了一些东西,但这对我有用。
Here is how I did it...
First of all if you are using SimpleCursorAdapter you dont need to provide a full implementation (newView) that inflates views - it does it for you. If you need to do that, then you should be deriving from CursorAdapter. Anyway...
I'm not sure how the Android system manages the check state of their view without having a selected item position on the adapter so I'm apparently missing something but this worked for me.