重写BaseAdapter的样例

(1)用于ListView,用来设置每项的背景

1 package com.studio.basf.libs;
2
3 import java.util.List;
4
5 import android.content.Context;
6 import android.view.LayoutInflater;
7 import android.view.View;
8 import android.view.ViewGroup;
9 import android.widget.BaseAdapter;
10 import android.widget.RelativeLayout;
11 import android.widget.TextView;
12
13 import com.studio.basf.android.R;
14
15 /**
16 * 自定义ListView适配器-Square
17 * @author lhb-JewLeo
18 */
19 public class SquareAdapter extends BaseAdapter {
20
21 private LayoutInflater mInflater;
22 private List<String> list;
23
24 //自定义容器类
25 class ViewHolder {
26 private TextView TvName;
27 private RelativeLayout ReLayout;
28
29 public TextView getTvName() {
30 return TvName;
31 }
32 public void setTvName(TextView tvName) {
33 TvName = tvName;
34 }
35 public RelativeLayout getReLayout() {
36 return ReLayout;
37 }
38 public void setReLayout(RelativeLayout reLayout) {
39 ReLayout = reLayout;
40 }
41 }
42
43 public SquareAdapter(List<String> list, Context context) {
44 this.list = list;
45 this.mInflater = LayoutInflater.from(context);
46 }
47
48 public int getCount() {
49 // TODO Auto-generated method stub
50 return list.size();
51 }
52
53 public Object getItem(int position) {
54 // TODO Auto-generated method stub
55 return list.get(position);
56 }
57
58 public long getItemId(int position) {
59 // TODO Auto-generated method stub
60 return position;
61 }
62
63 public View getView(int position, View convertView, ViewGroup parent) {
64 // TODO Auto-generated method stub
65 ViewHolder holder = null;
66 if (convertView == null) {
67 holder = new ViewHolder();
68
69 convertView = mInflater
70 .inflate(R.layout.square_item_view, null);
71 holder.setTvName((TextView) convertView
72 .findViewById(R.id.tvItemName));
73 holder.setReLayout((RelativeLayout) convertView
74 .findViewById(R.id.relativeSquare));
75
76 convertView.setTag(holder);
77 } else {
78 holder = (ViewHolder) convertView.getTag();
79 }
80
81 holder.getTvName().setText(list.get(position));
82 if (position == 0) {
83 holder.getReLayout().setBackgroundResource(R.drawable.circle_list_top);
84 } else if (position == getCount() - 1) {
85 holder.getReLayout().setBackgroundResource(R.drawable.circle_list_bottom);
86 } else {
87 holder.getReLayout().setBackgroundResource(R.drawable.circle_list_middle);
88 }
89
90 return convertView;
91 }
92
93 }

(2)动态改变ListView项中某个显示字段的内容



1 package com.studio.basf.libs;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import android.content.Context;
7 import android.view.LayoutInflater;
8 import android.view.View;
9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.TextView;
12
13 public class CustomListAdapter extends BaseAdapter {
14
15 private LayoutInflater mInflater;
16 private ArrayList<HashMap<String, String>> list;
17 private int resource;
18 private String[] from;
19 private int[] to;
20
21 // 自定义容器类
22 class ViewHolder {
23 private ArrayList<TextView> tvCollections;
24
25 public ArrayList<TextView> getTvCollections() {
26 return tvCollections;
27 }
28
29 public void setTvCollections(ArrayList<TextView> tvCollections) {
30 this.tvCollections = tvCollections;
31 }
32 }
33
34 public CustomListAdapter(Context context,
35 ArrayList<HashMap<String, String>> list, int resource,
36 String[] from, int[] to) {
37 this.list = list;
38 this.mInflater = LayoutInflater.from(context);
39 this.resource = resource;
40 this.from = from;
41 this.to = to;
42 }
43
44 public int getCount() {
45 // TODO Auto-generated method stub
46 return list.size();
47 }
48
49 public Object getItem(int position) {
50 // TODO Auto-generated method stub
51 return list.get(position);
52 }
53
54 public long getItemId(int position) {
55 // TODO Auto-generated method stub
56 return position;
57 }
58
59 public View getView(int position, View convertView, ViewGroup parent) {
60 // TODO Auto-generated method stub
61 ViewHolder holder = null;
62 if (convertView == null) {
63 holder = new ViewHolder();
64 convertView = mInflater.inflate(resource, null);
65
66 ArrayList<TextView> tvList = new ArrayList<TextView>();
67 for (int i = 0; i < to.length; i++) {
68 tvList.add((TextView) convertView.findViewById(to[i]));
69 }
70 holder.setTvCollections(tvList);
71
72 convertView.setTag(holder);
73 } else {
74 holder = (ViewHolder) convertView.getTag();
75 }
76
77 // 获取单项数据
78 HashMap<String, String> hashMap = list.get(position);
79
80 if (hashMap.get("isValid").trim().equals("失败")) {
81 hashMap.remove("showvalidpoint");
82 }
83
84 // 绑定到视图上
85 for (int i = 0; i < to.length; i++) {
86 holder.getTvCollections().get(i).setText(hashMap.get(from[i]));
87 }
88
89 return convertView;
90 }
91 }
posted @ 2011-06-01 16:45  网络渔夫  阅读(1808)  评论(0编辑  收藏  举报