Android的定制微调:检查单选按钮中选择 [英] Android Custom Spinner: Check Radio Button on Select

查看:206
本文介绍了Android的定制微调:检查单选按钮中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义适配器为我的微调,因为我想有多个行。不过,我想基本上像重建具有当选择了微调项是被选中的单选按钮,机器人微调模式。一切工作只是罚款我不明白怎么当选择列表项检查单选按钮。

我SimpleCursorAdapter定义适配器code:

  @覆盖
公共查看newDropDownView(上下文的背景下,光标光标的ViewGroup父){
    super.newDropDownView(背景下,游标,父母);    查看查看= View.inflate(背景下,R.layout.grain_spinner_row,NULL);
    INT名称列= cursor.getColumnIndex(名称);
    字符串的getName = cursor.getString(名称列);
    TextView的名字=(TextView中)view.findViewById(R.id.GrainSpinnerName);
    name.setText(的getName);    INT loviColumn = cursor.getColumnIndex(罗维朋比);
    字符串getLovi = cursor.getString(loviColumn);
    TextView的lovi =(TextView中)view.findViewById(R.id.GrainSpinnerLovibond);
    lovi.setText(getLovi);    INT gravityColumn = cursor.getColumnIndex(重力);
    字符串getGravity = cursor.getString(gravityColumn);
    TextView的比重=(TextView中)view.findViewById(R.id.GrainSpinnerGravity);
    gravity.setText(getGravity);    RB =(单选)view.findViewById(R.id.GrainSpinnerRadio);    返回视图。
}公共静态无效toggleR​​adio(){
    如果(!(rb.isChecked())){
        rb.toggle();
    }
}

OnSelectedItemChanged code:

  @覆盖
    公共无效onItemSelected(适配器视图<>母公司,视图V,INT位置,长的id){
        //从当前选择的项目收集的ID,所以我们可以用它来引用该成分的配方数据库。
        //因为我们使用的是SimpleCursorAdapter填充nameSpinner,我们必须使用游标来请求currentName。
        光标CC =(光标)(nameSpinner.getSelectedItem());
        currentName = cc.getString(cc.getColumnIndex(名字));
        SQL字符串=SELECT * FROM WHERE粮食名='+ currentName +'和起源='+ currentOrigin +';
        游标数据= database.rawQuery(SQL,NULL);
        data.moveToFirst();        INT名称列= data.getColumnIndex(名称);
        INT loviColumn = data.getColumnIndex(罗维朋比);
        INT gravityColumn = data.getColumnIndex(重力);
        INT originColumn = data.getColumnIndex(原产地);        字符串currentIngredientName = data.getString(名称列);
        字符串currentIngredientLovi = data.getString(loviColumn);
        字符串currentIngredientGravity = data.getString(gravityColumn);
        字符串currentIngredientOrigin = data.getString(originColumn);        addIngredient =新的String [4];
        addIngredient [0] = currentIngredientName;
        addIngredient [1] = currentIngredientLovi;
        addIngredient [2] = currentIngredientGravity;
        addIngredient [3] = currentIngredientOrigin;GrainSpinnerAdapter.toggleR​​adio();
    }

LogCat中是给我在nullex pression错误:

  GrainSpinnerAdapter.toggleR​​adio();

 如果(!(rb.isChecked())){


解决方案

如果你的问题很简单,因为它似乎(仅仅改变在微调选择的单选按钮的状态),你可以调用切换()方法收音机按钮,将反转单选按钮的状态。你也可以声明单选按钮,并调用一个方法器isChecked

 单选myRadioButton =(单选)findViewById(R.id.My_RadioButton);公共无效YourSpinnerItemMethod(){
    如果(!(myRadioButton.isChecked()))
         myRadioButton.toggle();
}

编辑:

  OnSelectedItemChange {
  YOUR_ code;
  Class_Containing_Radio_Button.toggleButton();
}Class_Containing_Radio_Button.class {
  单选按钮myButton的;  切换按钮(){
    myButton.toggle();
  }  的onCreate(){
    则myButton =(单选)findViewById(R ........);
  }
}

那么你也可以有反应用器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())){

解决方案

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.

RadioButton myRadioButton = (RadioButton) findViewById(R.id.My_RadioButton);

public void YourSpinnerItemMethod () {
    if ( !(myRadioButton.isChecked()) )
         myRadioButton.toggle();
}

EDIT:

OnSelectedItemChange {
  YOUR_CODE;
  Class_Containing_Radio_Button.toggleButton();
}

Class_Containing_Radio_Button.class {
  RadioButton myButton;

  toggleButton() {
    myButton.toggle();
  }

  onCreate() {
    myButton = (RadioButton) findViewById(R........);
  }
}

then you can also have a method that reacts to the RadioButton's check state using the isChecked() method. Good Luck!

这篇关于Android的定制微调:检查单选按钮中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆