目前接触ListView控件比较多,而该控件想要达到一定的效果就要自行定义适配器(Adapter),说到适配器就想起ASP.NET的 GridView控件的适配器,当时的理解就是要给适配器提供一个数据源,然后GridView控件来引用这个适配器。这样GridView就能把数据源的数据显示出来。
Android的适配器的道理应该是一样的吧。
BaseAdapter的定义:
public abstract class
BaseAdapter
extends Object
implements ListAdapter SpinnerAdapter
Common base class of common implementation for an Adapter
that can be used in both ListView
(by implementing the specialized ListAdapter
interface} and Spinner
(by implementing the specialized SpinnerAdapter
interface.
BaseAdapter为抽象类,必须写一个自定义的适配器继承之该类:下面为默认继承BaseAdapter抽象类后系统提示需要完成的工作代码
private class MyAdapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return 0; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return null; } }
1:getCount方法返回数据的条数;
2:getItem方法Get the data item associated with the specified position in the data set.
3:getItemId方法Get the row id associated with the specified position in the list.
4:getView方法为最重要的方法,也是需要编写大量代码的方法该,方法直接返回每个ListView单元格显示的内容(View);Get a View that displays the data at the specified position in the data set.
下面列举两个自定义适配器代码:
代码一:
private class MyAdapter extends BaseAdapter{ @Override public int getCount() { // 这里我就返回10了,也就是一共有10项数据项 return 10; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // position就是位置从0开始,convertView是ListView中每一格要显示的view //通常return 的view也就是convertView //parent就是父窗体了,也就是ListView了. //这里每个单元格只返回一个TextView,文本设置为"朱启测试" TextView mTextView = new TextView(getApplicationContext()); mTextView.setText("朱启测试"); mTextView.setTextColor(Color.RED); return mTextView; } } }
代码二:比代码一稍微复杂点。该适配器返回每个单元的内容包含数据的年、月、日、标题、简介。配合单独的R.layout.columns_item布局文件可自定义想要的单元格效果。
public class MySimpleAdapterCol extends BaseAdapter { private LayoutInflater inflater = null; private Context context = null; private List<Map<String, String>> pathList = null; public MySimpleAdapterCol(Context context, List<Map<String, String>> pathList) { this.context = context; this.pathList = pathList; inflater = LayoutInflater.from(context); } public int getCount() { return pathList != null ? pathList.size() : 0; } public Object getItem(int arg0) { return pathList.get(arg0); } public long getItemId(int position) { return position % pathList.size(); } public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.columns_item, null); } TextView tv_col_date = (TextView) convertView .findViewById(R.id.col_date); TextView tv_col_month = (TextView) convertView .findViewById(R.id.col_month); TextView tv_col_year = (TextView) convertView .findViewById(R.id.col_year); TextView tv_col_title = (TextView) convertView .findViewById(R.id.col_title); TextView tv_col_content = (TextView) convertView .findViewById(R.id.col_content); String str_date = pathList.get(position).get("pubDate"); tv_col_date.setTextSize(18); tv_col_date.setTextColor(Color.BLUE); tv_col_date.setText(str_date.substring(str_date.length() - 2, str_date.length())); tv_col_month.setText(str_date.substring(str_date.length() - 4, str_date.length() - 2)); tv_col_year.setText(str_date.substring(0, 4)); tv_col_title.setText(pathList.get(position).get("title")); tv_col_content.setText(pathList.get(position).get("summary")); return convertView; } }
最后:当然,从BaseAdapter的定义可以看出,自定义的适配器不仅仅只使用于ListView控件,还可定义Spinner控件。
附加上SimpleAdapterd 应用小实例
for (int i = 0; i < 10; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("PIC", R.drawable.pic); map.put("TITLE", "Test Title"); map.put("CONTENT", "Test Content"); contents.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, (List<Map<String, Object>>) contents, R.layout.listitem, new String[] { "PIC", "TITLE", "CONTENT" }, new int[] { R.id.listitem_pic, R.id.listitem_title, R.id.listitem_content }); listView.setAdapter(adapter);