public class SpinnerActivity extends Activity {
Spinner spinner_c;
Spinner spinner_2;
private ArrayAdapter<String> aspnCountries;
private List<String> allcountries;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("SpinnerActivity");
setContentView(R.layout.spinner);
find_and_modify_view();

}

private static final String[] mCountries = { "China" ,"Russia", "Germany",

"Ukraine", "Belarus", "USA" };

 

private void find_and_modify_view() {

//第一种方法,用数组的方法动态传入值

spinner_c = (Spinner) findViewById(R.id.spinner_1);
allcountries = new ArrayList<String>();
for (int i = 0; i < mCountries.length; i++) {
allcountries.add(mCountries[i]);
}

aspnCountries = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, allcountries);

aspnCountries.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner_c.setAdapter(aspnCountries);

//第二种方法,通过资源文件固定初始化值。

spinner_2 = (Spinner) findViewById(R.id.spinner_2);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner_2.setAdapter(adapter);

}

最后修改:2011 年 07 月 08 日
一分也是爱