如何更改 Spinner 使用的字符串数组?

发布于 2024-11-15 01:15:31 字数 1304 浏览 2 评论 0原文

我正在尝试更改我的两个 Spinner 的内容。所以我已经创建了我的 Spinners:

       Spinner spinner01 = (Spinner) findViewById(R.id.Spinner01);
    Spinner spinner02 = (Spinner) findViewById(R.id.Spinner02);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);


    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner01.setAdapter(adapter1);
    spinner02.setAdapter(adapter2);

    spinner01.setOnItemSelectedListener(new MyOnItemSelectedListener1());
    spinner02.setOnItemSelectedListener(new MyOnItemSelectedListener2());

现在,通过不同的方法,我希望能够执行以下操作:

    adapter1 = ArrayAdapter.createFromResource(
            this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
    adapter2 = ArrayAdapter.createFromResource(
            this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);

是否有办法让适配器从不同的字符串数组中读取?或者我应该用新值替换字符串数组中的值?

I'm trying to change the contents of my two Spinners. So I've got my Spinners created:

       Spinner spinner01 = (Spinner) findViewById(R.id.Spinner01);
    Spinner spinner02 = (Spinner) findViewById(R.id.Spinner02);
    ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
            this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);


    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner01.setAdapter(adapter1);
    spinner02.setAdapter(adapter2);

    spinner01.setOnItemSelectedListener(new MyOnItemSelectedListener1());
    spinner02.setOnItemSelectedListener(new MyOnItemSelectedListener2());

Now from a different method, I want to be able to do something like this:

    adapter1 = ArrayAdapter.createFromResource(
            this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
    adapter2 = ArrayAdapter.createFromResource(
            this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);

Is there anyway to get the adapters to read from a different String Array? Or should I be replacing the values in the String Array with new ones?

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

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

发布评论

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

评论(3

舂唻埖巳落 2024-11-22 01:15:31

为您希望分配给 spinner2 的每个阵列创建单独的适配器。
宣布它们为最终版本。
在设置 spinner1 ItemSelected Listener 之前放置适配器声明。
在 spinner1 ItemSelected 侦听器中,根据需要设置适当的适配器。

在下面的代码中我有两个旋转器。
spinner1 有 4 个包含传感器类型的条目,当选择其中一种传感器类型时,spinner2 将填充该传感器类型的各种模型。

public class MainActivity extends Activity {

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   final Spinner s1 = (Spinner) findViewById(R.id.spinner_sensor_type);
   final Spinner s2 = (Spinner) findViewById(R.id.spinner_sensor_model);
   final ArrayAdapter<CharSequence> sensor_adapter = ArrayAdapter.createFromResource(
            this, R.array.sensors,android.R.layout.simple_spinner_item);
    sensor_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    final ArrayAdapter<CharSequence> pa_adapter = ArrayAdapter.createFromResource(
            this, R.array.pa_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ca_adapter = ArrayAdapter.createFromResource(
            this, R.array.ca_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ni_adapter = ArrayAdapter.createFromResource(
            this, R.array.ni_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ni_fe_adapter = ArrayAdapter.createFromResource(
            this, R.array.ni_fe_models,android.R.layout.simple_spinner_item);
    pa_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ca_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ni_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ni_fe_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(sensor_adapter); 
    s2.setAdapter(pa_adapter); // set default array values in spinner 2 
    s1.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Sensor Type: position=" + position + " id=" + id);
                    switch (position) {
                    case 0: 
                        s2.setAdapter(pa_adapter);                          
                            break;
                    case 1: 
                        s2.setAdapter(ca_adapter);
                            break;
                    case 2:
                        s2.setAdapter(ni_adapter);
                            break;
                    case 3:
                        s2.setAdapter(ni_fe_adapter);
                    default: // Do this...
                            break;
               }  

                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Sensor Type: unselected");
                }
            });



    s2.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Sensor Model: position=" + position + " id=" + id);
                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Sensor Model: unselected");
                }
            });
}

}

Create separate adapters for each array you wish to assign to spinner2.
Declare them as final.
Place the adapter declarations before you set spinner1 ItemSelected Listener.
In spinner1 ItemSelected listener, set the appropriate adapter as needed.

In the code below I have two spinners.
spinner1 has 4 entries containing sensor types, when one of the sensor types is selected spinner2 is then populated with the various models for that sensor type.

public class MainActivity extends Activity {

void showToast(CharSequence msg) {
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   final Spinner s1 = (Spinner) findViewById(R.id.spinner_sensor_type);
   final Spinner s2 = (Spinner) findViewById(R.id.spinner_sensor_model);
   final ArrayAdapter<CharSequence> sensor_adapter = ArrayAdapter.createFromResource(
            this, R.array.sensors,android.R.layout.simple_spinner_item);
    sensor_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    final ArrayAdapter<CharSequence> pa_adapter = ArrayAdapter.createFromResource(
            this, R.array.pa_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ca_adapter = ArrayAdapter.createFromResource(
            this, R.array.ca_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ni_adapter = ArrayAdapter.createFromResource(
            this, R.array.ni_models,android.R.layout.simple_spinner_item);
    final ArrayAdapter<CharSequence> ni_fe_adapter = ArrayAdapter.createFromResource(
            this, R.array.ni_fe_models,android.R.layout.simple_spinner_item);
    pa_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ca_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ni_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ni_fe_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1.setAdapter(sensor_adapter); 
    s2.setAdapter(pa_adapter); // set default array values in spinner 2 
    s1.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Sensor Type: position=" + position + " id=" + id);
                    switch (position) {
                    case 0: 
                        s2.setAdapter(pa_adapter);                          
                            break;
                    case 1: 
                        s2.setAdapter(ca_adapter);
                            break;
                    case 2:
                        s2.setAdapter(ni_adapter);
                            break;
                    case 3:
                        s2.setAdapter(ni_fe_adapter);
                    default: // Do this...
                            break;
               }  

                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Sensor Type: unselected");
                }
            });



    s2.setOnItemSelectedListener(
            new OnItemSelectedListener() {
                public void onItemSelected(
                        AdapterView<?> parent, View view, int position, long id) {
                    showToast("Sensor Model: position=" + position + " id=" + id);
                }

                public void onNothingSelected(AdapterView<?> parent) {
                    showToast("Sensor Model: unselected");
                }
            });
}

}

━╋う一瞬間旳綻放 2024-11-22 01:15:31

您可以使用 ArrayAdapter 的 getter /设置者:

例如:

adapter1.clear();
adapter1.addAll(theNewListOfValues);
adapter1.notifyDatasetChanged();

You can use the ArrayAdapter's getters/setters:

E.g:

adapter1.clear();
adapter1.addAll(theNewListOfValues);
adapter1.notifyDatasetChanged();
十雾 2024-11-22 01:15:31

您应该能够使用新适配器在微调器上调用 setAdapter 。我建议您在创建适配器后将它们存储为活动中的字段,以便在需要再次在适配器之间交换时可以轻松地重复使用它们。

You should be able to just call setAdapter on your spinners with the new adapters. I would suggest storing the adapters as fields in your activity after you create them so you can easily re-use them if you need to swap between the adapters again.

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