1.方式一
private String[] data = {"AAA","BBB","CCC"};
spinner1 = findViewById(R.id.spinner);
spinner1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,
android.R.id.text1,
data));
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.v("1", data[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2.方式二:可以自定义Adapter
ArrayList<String> datas=new ArrayList<String>();
for (int i = 0; i < 10; i++) {
datas.add("AAA:"+i);
}
//
MyAdapter adapter=new MyAdapter(datas);
spinner1.setAdapter(adapter);
// 自定义Adapter继承BaseAdapter
public class MyAdapter extends BaseAdapter {
private ArrayList<String> mDatas;
public MyAdapter(ArrayList<String> datas) {
mDatas=datas;
}
@Override
public int getCount() {
return mDatas.size();
}
// 监听点击
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.v("ldx", "getView position="+position);
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.lv_item_layout, null);
TextView tv=(TextView) view.findViewById(R.id.tv);
tv.setText(mDatas.get(position));
return view;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
3.lv_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#F00"
android:gravity="center"
android:id="@+id/tv" />