2012-08-03 84 views
2

我對此很新,而且我有一個可能非常簡單的問題。從字符串數組中填充微調器

我有3個紡紗器,我想根據用戶在以前的紡紗器上所做的選擇來填充每個紡紗器。

現在我已經設置了顯示所選數據的烤麪包,但是我想設置一個活動來打開,但那是晚點。現在我需要知道如何從strings.xml中的數組填充spinner。

的.java

import java.util.ArrayList; 
import java.util.List; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.Toast; 

/** 
* @author madonk 
* 
*/ 
public class Region extends Activity { 

Spinner sp1,sp2,sp3; 
ArrayAdapter<String> reg_adp,sw_city_adp,sw_lake_charles_adp; 
List<String> regions,sw_cities,sw_lake_charles; 
int pos; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_region); 

    regions=new ArrayList<String>(); 

    regions.add("Select a Region"); 
    regions.add("Southwest"); 

    sp1= (Spinner) findViewById(R.id.regions_spinner); 
    sp2= (Spinner) findViewById(R.id.sw_city_spinner); 
    sp3= (Spinner) findViewById(R.id.sw_lake_charles_spinner); 

    reg_adp=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,regions); 
    reg_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
    sp1.setAdapter(reg_adp); 

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> arg0, View arg1, 
       int arg2, long arg3) { 
      // TODO Auto-generated method stub 
      pos=arg2; 
      add(); 

     } 

     private void add() { 
      // TODO Auto-generated method stub 
      Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show(); 

      switch(pos) 
      { 
      case 0: 
       sw_cities= new ArrayList<String>();      
       sw_cities.add("Select a City"); 


       sw_city_adp=new ArrayAdapter<String>(Region.this, 
         android.R.layout.simple_dropdown_item_1line,sw_cities); 
       sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
       sp2.setAdapter(sw_city_adp); 

       select(); 

       break; 
      case 1: 
       sw_cities= new ArrayList<String>(); 
       sw_cities.add("Select a City"); 
       sw_cities.add("Lake Charles"); 
       sw_cities.add("Iowa"); 
       sw_cities.add("Lake Arthur"); 

       sw_city_adp=new ArrayAdapter<String>(Region.this, 
         android.R.layout.simple_dropdown_item_1line,sw_cities); 
       sw_city_adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
       sp2.setAdapter(sw_city_adp); 

       select(); 

       break; 
      } 

     } 

     private void select() { 
      // TODO Auto-generated method stub 

      sp2.setOnItemSelectedListener(new OnItemSelectedListener() { 

       public void onItemSelectedregions(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 

       } 

       public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 

       } 

       public void onItemSelected(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 

       } 

       public void onNothingSelectedregions(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 

       } 
      }); 

     } 

     public void onNothingSelectedcities(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 
    }); 
    } 

stings.xml

<resources> 

    <string name="app_name">Louisiana Festivals</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title_activity_region">Select a Region</string> 
    <string-array name="regions_array"> 
     <item >Southwest</item> 
    </string-array> 
     <string name="select_a_region">Select a Region</string> 
    <string-array name="southwest_cities"> 
     <item >Lake Charles</item> 
     <item >Iowa</item> 
    </string-array> 
    <string name="sw_select_a_city">Select a City</string> 
    <string-array name="lake_charles"> 
     <item>Contraband days Pirate Festival</item> 
     <item>Other festival to be determined</item> 
    </string-array> 

</resources> 

回答

5

你可以從strings.xml中使用getStringArray()一個字符串數組。因此,代碼片斷你想要的是:

String[] regionsArray = getResources().getStringArray(R.array.regions_array); 
regions = new ArrayList<String>(Arrays.asList(regionsArray)); 

現在,無論你進入regions_array將在地區ArrayList結束。

+0

任何想法如何使它到第二個微調不可用的地方,直到在第一個微調上進行選擇等等?或者應該是另一個問題? – Madonk 2012-08-04 00:07:43

+0

並且是getResources。一個局部變量? – Madonk 2012-08-04 00:13:34

+0

即時獲取getResources無法解析 – Madonk 2012-08-04 00:27:05