[功能]
* AdapterView
- ListView
- GridView
- Gallery
- Spinner
* Adapter
- SimpleAdapter
- SimpleCursorAdapter
- ArrayAdapter
至于 AdapterView & Adapter 如何选择的问题 有2点需要注意:
× AdapterView 的选择 只和界面有关 和具体数据无关
× Adapter 的选择 只喝数据有关 和界面无关
二者耦合度高 互不干涉!
android给出的AdapterView中所使用的Adapter的item都是TextView 即 只能显示一下文字信息 这就限制了它的应用 所以现在告诉大家怎么使用别的View
[思路]
1. 自定义一个 extends BaseAdapter 的 class 如 public class CustomList extends BaseAdapter
2. 填充 CustomList 的一些方法 如下:
- public int getCount()
- public Object getItem(int position)
- public long getItemId(int position)
- public View getView(int position, View convertView, ViewGroup parent)
public int getCount()
public Object getItem(int position)
public long getItemId(int position)
public View getView(int position, View convertView, ViewGroup parent)
[代码]
1. 比如 现在有下列数据 要求显示之
- String[] week = {
- "JAN","FEB","MAR","APR",
- "MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "
- };
String[] week = {
"JAN","FEB","MAR","APR",
"MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC "
};
2. 一些函数的定义如下
- public class CustomList extends BaseAdapter {
- Activity activity;
- //construct
- public CustomList(Activity a ) {
- activity = a;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return week.length;
- }
- @Override
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return week[position];
- }
- @Override
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- TextView tv = new TextView(activity);
- tv.setText(week[position]);
- return tv;
- }
- }
public class CustomList extends BaseAdapter {
Activity activity;
//construct
public CustomList(Activity a ) {
activity = a;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return week.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return week[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv = new TextView(activity);
tv.setText(week[position]);
return tv;
}
}
3. 考虑到美观 我们可以把getView()的一些填充提取出来 即 根据目标的position 得到目标所需View
- public View addCustomView(int position){
- View view = new View(activity);
- switch(position){
- case 11:
- Button btn = new Button(activity);
- btn.setText("Yes!");
- view = btn;
- case 12:
- ImageView iv = new ImageView(activity);
- iv.setImageResource(R.drawable.robot);
- view = iv;
- break;
- default:
- TextView tv = new TextView(activity);
- tv.setGravity(1);
- tv.setText(week[position]);
- tv.setPadding(5, 5, 5, 5);
- view = tv;
- }
- return view;
- }
public View addCustomView(int position){
View view = new View(activity);
switch(position){
case 11:
Button btn = new Button(activity);
btn.setText("Yes!");
view = btn;
case 12:
ImageView iv = new ImageView(activity);
iv.setImageResource(R.drawable.robot);
view = iv;
break;
default:
TextView tv = new TextView(activity);
tv.setGravity(1);
tv.setText(week[position]);
tv.setPadding(5, 5, 5, 5);
view = tv;
}
return view;
}
4. 如何使用
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- return addCustomView(position);
- }
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return addCustomView(position);
}
这样 我们如果要定制某个position的View 就可以增加相应的case..
done!