2015-03-30 112 views
-1

我有3個紡紗如何基於第一個微調器和第二個微調器來填充第二個微調器?

<string-array name="country_array"> 
     <item>India</item> 
     <item>Pakistan</item> 
     <item>Srilanka</item> 
    </string-array> 

<string-array name="city_array"> 
     <item>Bangalore</item> 
     <item>Hyderabad</item> 
     <item>Delhi</item> 
    </string-array> 

<string-array name="area_array"> 
     <item>Hitech City</item> 
     <item>Jubli Hills</item> 
     <item>Banjara Hills</item> 
    </string-array> 

如何讓下拉列表?選擇印度時,它應該填充班加羅爾,海得拉巴和德里,並且在選擇海得拉巴應該填充海德拉巴科技城,jublihills等提前

謝謝

+1

因此,以你的xml例子爲例,你如何確定哪個區域對應於哪個城市,哪個城市對應哪個國家?您的子元素沒有對父母的引用。 – 2015-03-30 19:40:42

回答

1

你可以設置一個特定的陣列到第1微調,然後用功能檢查的微調選擇的串並給另一個數組到第二微調,這樣的:

String []ejemplos= { "option1", "option2", "option3"}; 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ejemplos); 
     Sp1.setAdapter(adapter); 

      Sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
       String item = (String)parent.getItemAtPosition(pos); 

       if(item.matches("option 1")){ 
        con=1; 
        checar(); 
       }else{ 
        if(item.matches("option 2")){ 
         con=2; 
         checar(); 
        } 

       } 
      } 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 

然後,功能,填補了第二微調(SP2),這樣的事情:

void checar(){ 
     if(con==1){ 
      String []ejemplos2= { "option 1", "option 2", "option 3"}; 
      ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ejemplos2); 
      Sp2.setAdapter(adapter2); 
     }else{ 
      if(con==2){ 
       String []ejemplos2= { "opcion 1"}; 
       ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ejemplos2); 
       Sp2.setAdapter(adapter2); 
      }else{ 
       if(con==3){ 
        String []ejemplos2= { "opcion 1", "opcion 2", "opcion 3"}; 
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, ejemplos2); 
        Sp2.setAdapter(adapter2); 
       } 
      } 
     } 
    }//fin checar 

和一個監聽器添加到第二微調是這樣的:在

Sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
       public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
        con2 = (String)parent.getItemAtPosition(pos); 
        if(con2=="opcion 1" && con==1){ 
         //do something 
        }else{ 
         if(con2=="opcion 2" && con==1){ 
          //do something 
         } 
        } 
       } 
       public void onNothingSelected(AdapterView<?> parent) { 
       } 
      }); 

第二個微調,你可以再次調用該函數,並將字符串設置爲第三個微調,然後做任何你想做的事!希望這對你有用!

看到你!

相關問題