Android 自定义微调器:检查选择上的单选按钮

发布于 2024-11-26 04:50:09 字数 2909 浏览 4 评论 0原文

我为我的微调器创建了一个自定义适配器,因为我想要多行。但是,我基本上想使用选择微调器项目时选择的单选按钮重新创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉城凉梦凉人心 2024-12-03 04:50:09

如果您的问题像看起来一样简单(仅在选择微调器时更改单选按钮的状态),您可以在单选按钮上调用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 的检查状态做出反应的方法。祝你好运!

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!

不美如何 2024-12-03 04:50:09

我是这样做的......

首先,如果您使用 SimpleCursorAdapter,您不需要提供一个完整的实现(newView)来膨胀视图 - 它会为您做到这一点。如果您需要这样做,那么您应该从 CursorAdapter 派生。无论如何...

  • 在项目的 XML 中,使用 CheckedTextView 作为单选按钮(这是 Android spinner 使用的)
  • 在适配器上公开 SelectedItemPosition 属性
  • 在适配器中重写 BindView() 并根据以下内容设置 CheckedTextView 的检查状态当前选定项目的位置
  • 在活动中,在需要时调用设置器来设置适配器 SelectedItemPosition

我不确定 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...

  • In your XML for an item, use a CheckedTextView for the radio button (this is what the Android spinner uses)
  • On your adapter expose a SelectedItemPosition property
  • In the adapter override BindView() and set the check state of CheckedTextView based on the current selected item position
  • In the activity, call the setter to set the adapter SelectedItemPosition whenever needed

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文