2012-08-04 45 views
1

我是Android開發新手,所以查詢看起來似乎是新手。Android - 爲Spinners動態添加值

我試圖做一個應用程序,其中有一些spinners,並且在用戶選擇一個spinner後,另一個spinner會根據spinner1中的選擇填充數據。

即。第一個用戶選擇國家。在國家的基礎上,微調器2得到了充足的狀態,並根據國家的規定,微調器3被城市填滿。

我已經創建了所有的國家作爲一個字符串數組中的strings.xml像 -

<string-array name="Country"> 
    <item >USA</item> etc... 

同樣狀態的string.xml爲已創建 - (每個國家是有命名約定一個單獨的條目如(國名)_(美國)

<string-array name="USA_state"> 
    <item >New York</item> etc... 

現在我想第二微調填充設在國家選擇因此,通過我所用的公式是將獲得第二個微調是 -

String getstate = country[index]+"_"+"state"; 
state=getResources() 
.getStringArray(getResources(). 
getIdentifier(getstate,null,getPackageName())); 

當我運行這個應用程序時,Manactivity只顯示一個空白屏幕,我無法獲得任何調試信息,並得到一個錯誤「jar文件android.jar沒有附加源代碼」。

請幫忙。下面的完整mainActivity給出 -

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

public class MainActivity extends Activity { 
String[] country; 
String[] state; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    country = getResources().getStringArray(R.array.country); 
    Spinner country_spinner =(Spinner) findViewById(R.id.country); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country); 
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 
     public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){ 
      int index = arg0.getSelectedItemPosition(); 
      Toast.makeText(getBaseContext(), "You have Selected"+country[index]+"country", Toast.LENGTH_SHORT).show(); 
      Fill (index); 
     } 
     public void onNothingSelected(AdapterView<?> arg0) {} 
    }); 


} 

protected void Fill(int index) { 
    // TODO Auto-generated method stub 
    //Field resField=R.array.getField(country[index]); 
    //int getstate = resField.getInt(null); 
    String getstate = country[index]+"_"+"state"; 
    state=getResources().getStringArray(getResources().getIdentifier(getstate,null,getPackageName())); 
    Spinner state_spinner = (Spinner) findViewById(R.id.state); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, state); 
    state_spinner.setAdapter(adapter); 
    state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){ 
      int index2 = arg0.getSelectedItemPosition(); 
      Toast.makeText(getBaseContext(),"You Have Selected "+state[index2] + " statet",Toast.LENGTH_SHORT).show(); 
     } 
     public void onNothingSelected(AdapterView<?> arg0){} 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

回答

2

我能解決這個問題。最終的代碼如下。這是一個嵌套的方法,所以如果有3個紡紗廠(像州之後的城市),我們是否再次爲該紡紗廠嵌套代碼?有沒有更簡單的方法?

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, country); 
    country_spinner.setAdapter(adapter); 
    country_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 

     public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){ 
      int index = arg0.getSelectedItemPosition(); 
      Toast.makeText(getBaseContext(), "You have Selected"+country[index]+" County", Toast.LENGTH_SHORT).show(); 

      final CharSequence[] state_array = state.getTextArray(index); 
      final Spinner state_spinner =(Spinner) findViewById(R.id.state); 
      ArrayAdapter<CharSequence> adapter1 = new ArrayAdapter<CharSequence>(MainActivity.this, android.R.layout.simple_spinner_item, state_array); 
      state_spinner.setAdapter(adapter1); 
      state_spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 

       public void onItemSelected (AdapterView<?> arg0, View arg1, int arg2, long arg3){ 
        final int index = arg0.getSelectedItemPosition(); 
        Toast.makeText(getBaseContext(), "You have Selected"+state_array[index]+" State", Toast.LENGTH_SHORT).show(); 

     }); 

       } 
       public void onNothingSelected(AdapterView<?> arg0) { 
        state_spinner.setAdapter(null); 
       } 
      });