如何将一个微调器项与第二个微调器项相关联

时间:2017-08-05 05:37:28

标签: android

2 [在此图片中,当我点击它时,微调器值不显示项目显示,当我选择没有在微调器上显示的项目但值将转到服务器时]

1 [这是第二个图像,其中是微调器项目]

我想实现一个代码,其中我使用了两个微调器,并且在下面的代码中给出了Items,现在我希望如果我在第一个微调器中没有选择,那么第二个微调器将只显示无项目,如果我在第一个微调器中选择1然后第二个微调器将只显示无和1项。谁能帮我提前谢谢你

       <Spinner
        android:background="@drawable/spinner_back"
        android:id="@+id/NumberOfBro"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:entries="@array/no_of_brothers"
        style="@style/Base.Widget.AppCompat.Spinner.Underlined"/>


        <Spinner
        android:background="@drawable/spinner_back"
        android:id="@+id/NumberOfMarriedBrother"
        android:layout_width="350dp"
        android:layout_height="50dp"
        android:entries="@array/no_of_married_Brothers"
        style="@style/Base.Widget.AppCompat.Spinner.Underlined"
        android:layout_below="@+id/NumberOfBro"/> 

        <string-array name="no_of_brothers">
          <item>Select Number of Brothers</item>
          <item>None</item>
          <item>1</item>
          <item>2</item>
          <item>3</item>
          <item>4</item>
          <item>4+</item>
       </string-array>

       <string-array name="no_of_married_Brothers">
         <item>Select Number of Married Brother</item>
         <item>None</item>
         <item>1</item>
         <item>2</item>
         <item>3</item>
         <item>4</item>
         <item>4+</item>
       </string-array>

这是我用于此微调器的java代码

  Spinner noOfBro = (Spinner) findViewById(R.id.noOfBro);
  Spinner noOfMBros = (Spinner) findViewById(R.id.noOfMBros);

  noOfBro.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
  {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, 
        int position, long l) {
        String[]resList=getResources().getStringArray
        (R.array.no_of_merried_brothers);
        String[] spList = new String[position + 1];
            for (int i = 0; i <= position; i++) {
            spList[i] = resList[i];
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<>
            (getApplicationContext(),
            android.R.layout.simple_spinner_item, spList);
            noOfMBros.setAdapter(adapter);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

1 个答案:

答案 0 :(得分:0)

您必须以编程方式执行此操作。在第一个微调器的OnItemSelectedListener事件中,创建第二个微调器的数组并设置为它:

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
        String[] resList = getResources().getStringArray(R.array.no_of_married_Brothers);
        String[] spList = new String[position + 1];

        for (int i = 0; i <= position; i++) {
            spList[i] = resList[i];
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),
                android.R.layout.simple_spinner_item, spList);
        sp2.setAdapter(adapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
});