简体   繁体   English

从数组中填充Spinner1,然后根据第一个Spinner选择为Spinner2选择第二个数组

[英]Spinner1 populate from array, then select second array for spinner2 based on 1st spinner selection

Okay spinner 1 uses Manu_array spinner 2 uses Manu 1xsd, Manu 2xrsd or 3x4rsd all I need for now is to be able to populate the second spinner, after selecting the a value from the Manu_array in spinner 1. 好吧,微调器1使用Manu_array微调器2使用Manu 1xsd,Manu 2xrsd或3x4rsd现在,我现在需要的是能够填充第二个微调器,在微调器1中从Manu_array中选择一个值之后。

<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>

<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>

<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>

<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>

My java file code: 我的Java文件代码:

 final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};

  // Create an ArrayAdapter using the string array and a default spinner     layout
  ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
  R.array.Manu_array, R.layout.textview);

  spinner1[0].setAdapter(adapter);
  // Specify the layout to use when the list of choices appears
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 // Apply the adapter to the spinner
    spinner1[0].setAdapter(adapter);

 spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

 public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {

            int index = arg0.getSelectedItemPosition();
            // storing string resources into Array
            Manu= getResources().getStringArray(R.array.Manu_array);

            Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
                    Toast.LENGTH_SHORT).show();

       }

        public void onNothingSelected(AdapterView<?> arg0) {
            // do nothing
        }
    });

I can process code that will allow the spinner to populate with the array data, but I cant get the second spinner to populate based on the selection of the first spinner, surely its simple if equals value use array. 我可以处理允许微调器使用数组数据填充的代码,但是我无法根据第一个微调器的选择来获取第二个微调器,当然,如果等于值则使用数组,这肯定很简单。

The spinner above shows the values for the Manu_array, the next spinner has to take the value selected and populate with the appropriate array values, but all I have managed to do is generate a second spinner with the array values I have manual typed in, not selected. 上面的微调器显示Manu_array的值,下一个微调器必须获取选定的值并使用适当的数组值填充,但是我设法做的就是生成第二个微调器,该数组具有我手动输入的数组值,而不是已选择。

if(Manu.equals("Manu 1xsd")){spinner2  languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......

Any suggestions where I may look for examples please looked at a few on here and confused me something chronic. 我可能在其中寻找示例的任何建议,请在此处查看一些内容,并让我感到有些困惑。

Set the second spinner adapter when an item from the first spinner is clicked 单击第一个微调器中的项目时,设置第二个微调器适配器

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        int arrayId = 0;
        switch(position) {//get array id according to selected position
            case 0:
               arrayId = R.array.Manu_1xsd;
               break;
            case 1:
               arrayId = R.array.Manu_2xrsd;
               break;
            case 2:
               arrayId = R.array.Manu_3x4rsd;
               break;
        };
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);

        spinner2.setAdapter(adapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM