https://www.cnblogs.com/demoMeng/p/6115289.html
适配器的使用(listview,recycleview,spinner都是这样用的,所有的适配器)
简单版:
创建下拉框的数据源
1 2 3 4 5 |
List<String> list = new ArrayList<String>(); list.add("苹果"); list.add("香蕉"); list.add("橘子"); list.add("香蕉"); |
创建适配器(下拉框的数据源是来自适配器)
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
为适配器添加样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
系统提供的样式如下
simple_spinner_dropdown_item(列表-间距较高比较好看)
simple_spinner_item(列表-间距紧凑不好看)
simple_list_item_checked(复选框-选中的有绿沟)
simple_list_item_single_choice (单选按钮)
向控件Spinner添加适配器
1 2 |
Spinner sp=(Spinner) findViewById(R.id.spinner1); sp.setAdapter(adapter); |
复杂版:
数据源准备:
list<自定义的也可以>
public class SpinnerDemo3Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int mode = getIntent().getIntExtra("mode", Spinner.MODE_DROPDOWN);
if (mode == Spinner.MODE_DROPDOWN) {
setContentView(R.layout.activity_spinner_mode_dropdown);
} else {
setContentView(R.layout.activity_spinner_mode_dialog);
}
//使用自定义的BaseAdapter
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
final List<Person> persons = new ArrayList<Person>();
persons.add(new Person("张三", "上海 "));
persons.add(new Person("李四", "上海 "));
persons.add(new Person("王五", "北京"));
persons.add(new Person("赵六", "广州 "));
// 建立Adapter绑定数据源
MyAdapter pAdapter = new MyAdapter(this, persons);
spinner3.setAdapter(pAdapter);
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SpinnerDemo3Activity.this, "--->" + persons.get(position).getName() + "--" + persons.get(position).getCity(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
实体对象Person:
public class Person {
private String name;
private String city;
public Person() {
}
public Person(String name,String city) {
super();
this.name = name;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
自定义适配器布局
MyAdapter: 主要是getview()
public class MyAdapter extends BaseAdapter {
private List<Person> mPersons;
public MyAdapter(Context context, List<Person> persons) {
mPersons = persons;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
#布局填充器
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.item_spinner_person, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvName.setText(mPersons.get(position).getName());
viewHolder.tvCity.setText(mPersons.get(position).getCity());
return convertView;
}
static class ViewHolder {
protected TextView tvName;
protected TextView tvCity;
ViewHolder(View rootView) {
initView(rootView);
}
private void initView(View rootView) {
tvName = (TextView) rootView.findViewById(R.id.tv_name);
tvCity = (TextView) rootView.findViewById(R.id.tv_city);
}
}
}