2017-07-05 132 views
0

我想通過從微調框中選擇一個國家來填充城市。它應該根據第一個微調者的選擇填充第二微調器的內容。我的strings.xml和MainActivity.java文件是這樣的:Android從另一個微調器填充微調內容

的strings.xml

<!-- Countries --> 
<string-array name="country_names"> 
    <item>Turkey</item> 
    <item>Spain</item> 
</string-array> 

<!-- Cities --> 
<string-array name="Turkey"> 
    <item>Istanbul</item> 
    <item>Adana</item> 
</string-array> 

<string-array name="Spain"> 
    <item>Madrid</item> 
    <item>Barcelona</item> 
</string-array> 

MainActivity.java

citySpinner = (Spinner) findViewById(R.id.input_city); 
    countrySpinner = (Spinner) findViewById(R.id.input_country); 

    countryAdapter = ArrayAdapter.createFromResource(this,R.array.country_names,android.R.layout.simple_spinner_item); 
    countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    final String[] country = new String[1]; 
    countrySpinner.setAdapter(countryAdapter); 
    countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) 
     { 
      country[0] = adapterView.getItemAtPosition(i).toString(); 
      Toast.makeText(mContext, adapterView.getItemAtPosition(i) + " selected", Toast.LENGTH_SHORT).show(); 
     } 

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

     } 
    }); 

    cityAdapter = ArrayAdapter.createFromResource(this,R.array.Turkey,android.R.layout.simple_spinner_item); 
    cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    citySpinner.setAdapter(cityAdapter); 

我需要給cityAdapter = ArrayAdapter.createFromResource(...)線內的變量,而不是書寫的土耳其或任何國家的名稱。

我不能拿出任何東西在網絡有用的,這是我最後的希望:)

回答

0

試試這個:

countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
       country[0] = adapterView.getItemAtPosition(i).toString(); 
       Toast.makeText(mContext, adapterView.getItemAtPosition(i) + " selected", Toast.LENGTH_SHORT).show(); 
       switch (i) { 
        case 0: 
         cityAdapter = ArrayAdapter.createFromResource(this, R.array.Turkey, android.R.layout.simple_spinner_item); 
         break; 
        case 1: 
         cityAdapter = ArrayAdapter.createFromResource(this, R.array.Madrid, android.R.layout.simple_spinner_item); 
         break; 
       } 
       cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       citySpinner.setAdapter(cityAdapter); 
      } 

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

     } 
    }); 
+0

它給出了錯誤** java.lang.NullPointerException:嘗試調用虛擬方法'void android.widget.ArrayAdapter.setDropDownViewResource(int)'對null行對象'cityAdapter.setDropDownViewResource(android.R。 ' –

+0

我編輯它現在嘗試 –

+0

仍然得到相同的錯誤 –

0

內第一微調的setonitemselected,宣佈第二微調,並呼籲clicklistener功能即second_spinner.setonitemselectedlistener那麼這兩個回調方法將在第一個微調器的setonitemselected方法內部實現。

+0

究竟怎麼了?你能證明嗎? –