第一种方法:
设置list
public List<String> getDataSource(){
List<String> list = new ArrayList<String>
List.add("beijing");
List.add("shanghai");
return list;
}
public class SpinnerActivity extends Activity{
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Overrride
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
findviewbyid;
adapter = new ArrayAdapter<String>(this,android.r.layout.simple_spinner.dropdown_item,getDataSource());
spinner.setAdapter(adapter);
}
}
第二种:
在values下建个array.xml
<resources>
<string-array name="spinmajor">
<item></item>
<item>计算机科学与技术</item>
<item>信息安全</item>
<item>软件工程</item>
</string-array>
</resources>
然后在布局文件中加上以下代码:
android:entries="@array/spinmajor"