Android 单微调器,多个数组

发布于 2024-12-12 01:44:03 字数 1824 浏览 0 评论 0原文

我无法在任何地方找到这个问题的答案(很可能我没有搜索正确)。在 Android 中,我试图在我的应用程序中创建一个微调器,根据选择的单选按钮从多个数组中提取。如果选择单选按钮 1,我希望微调器使用数组 1,如果选择单选按钮 2,我希望微调器使用数组 2。这是我到目前为止所做的,但它不起作用。每次我使用此代码单击应用程序中的选项卡时,我的应用程序都会强制关闭。

public class This_Activity extends Activity {
/** Called when the activity is first created. */

private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_2_layout);
    rb1=(RadioButton)findViewById(R.id.Radio1);
    rb2=(RadioButton)findViewById(R.id.Radio2);
    rb3=(RadioButton)findViewById(R.id.Radio3);}{




    if(rb1.isChecked() == true){

        Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array1, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);
    }

    if(rb2.isChecked() == true){

        Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array2, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    }

    if(rb3.isChecked() == true){

        Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array3, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner3.setAdapter(adapter);
    }


}

}

I haven't been able to find the answer to this anywhere (it's quite possible I'm not searching right). In Android I'm trying to make a single spinner in my app pull from multiple arrays depending on which radio button is selected. If radio button 1 is selected I want the spinner to use array 1, if radio button 2 is selected I want the spinner to use array 2. Here is what I have so far, but it doesn't work. Everytime I click on the tab in my app with this code my app force closes.

public class This_Activity extends Activity {
/** Called when the activity is first created. */

private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_2_layout);
    rb1=(RadioButton)findViewById(R.id.Radio1);
    rb2=(RadioButton)findViewById(R.id.Radio2);
    rb3=(RadioButton)findViewById(R.id.Radio3);}{




    if(rb1.isChecked() == true){

        Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array1, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter);
    }

    if(rb2.isChecked() == true){

        Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array2, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);
    }

    if(rb3.isChecked() == true){

        Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.Array3, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner3.setAdapter(adapter);
    }


}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

兮颜 2024-12-19 01:44:03

如果没有看到错误消息,则很难诊断问题。您可以编辑您的问题并包含这些内容吗?

然而,我注意到的一件事是,您在每种情况下都创建了不同的旋转器。尝试使用

Spinner spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = null;

上面的 if 语句。那么 if 语句的主体看起来就像

adapter = ArrayAdapter.createFromResource(
        this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

您可能还希望将侦听器连接到单选按钮,以便在用户单击它们时自动更改微调器,因为现在这只会在构造函数中发生。

    rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // change your spinner adapter here
        }
    });

将单选按钮放在 RadioGroup 中可能更容易OnCheckChangedListener ,因此您只需要一个侦听器来处理所有 3 个按钮。

It is difficult to diagnose the problem without the error messages you are seeing. Could you edit your question and include those?

However, one thing I am noticing is that you are creating different spinners in each of those cases. Try using

Spinner spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = null;

above your if statements. Then the body of your if statements will look like

adapter = ArrayAdapter.createFromResource(
        this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

you also probably want to hook up listeners to your radio buttons to change the spinner automatically when the user clicks on them, because as it is now this will only happen in the constructor.

    rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // change your spinner adapter here
        }
    });

it is probably easier to put your radio buttons in a RadioGroup and put the OnCheckChangedListener on that, so you only need one listener for all 3 buttons.

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