博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
重写BaseAdapter的样例
阅读量:5255 次
发布时间:2019-06-14

本文共 4845 字,大约阅读时间需要 16 分钟。

(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
}

转载于:https://www.cnblogs.com/jewleo/archive/2011/06/01/20110601_1.html

你可能感兴趣的文章
css3学习01
查看>>
【USACO】 奶牛会展
查看>>
ActiveMQ笔记之点对点队列(Point-to-Point)
查看>>
继承和多态
查看>>
Dijkstra+计算几何 POJ 2502 Subway
查看>>
修复IE不能执行JS的方法
查看>>
程序员究竟该如何提高效率zt
查看>>
希尔排序法(缩小增量法)
查看>>
PHP编程基础学习(一)——数据类型
查看>>
MongoDB-JAVA-Driver 3.2版本常用代码全整理(2) - 查询
查看>>
NPOI处理Word文本中上下角标
查看>>
Android笔记 Handler
查看>>
如何阅读大型前端开源项目的源码(转)
查看>>
java.util.Arrays类详解
查看>>
idea搭建tocmat
查看>>
NYOJ-626-intersection set(二分查找)
查看>>
Binary Tree Traversals HDU - 1710 
查看>>
PHP程序员的技术成长规划(送给迷茫的你)
查看>>
spring配置详解-连接池配置(转载)
查看>>
堆排序算法原理
查看>>