BaseAdapter是基本适配器,功能强大,凡是能用ArrayAdapter和SimpleAdapter解决的,都可以使用BaseAdapter解决。
BaseAdapter是一个抽象类,所以需要自定义一个类来继承BaseAdapter。BaseAdapter中所必须重写的方法的意义:
public int getCount():在此适配器中所代表的数据集中的条目数
public Object getItem(int position):获取数据集中与指定索引对应的数据项
public long getItemId(int position):取在列表中与指定索引对应的行id
public View getView(int position, View convertView, ViewGroup parent):很重要的一个方法。在ListView上显示的数据的逻辑就是写在这个方法里。
BaseAdapterListViewActivity2.java中的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package
com.zzh.day_listview;
import
java.util.ArrayList;
import
java.util.List;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.ImageView;
import
android.widget.ListView;
import
android.widget.TextView;
/**实现图片文字混合显示
* @author Administrator
*
*/
public
class
BaseAdapterListViewActivity2
extends
Activity
{
ListView lv;
List<String> list =
new
ArrayList<String>();
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.base_listview);
// 初始化list集合。
for
(
int
i =
0
; i <
20
; i++)
{
list.add(
"item -- "
+ i);
}
lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(
new
CustomBaseAdapter());
}
class
CustomBaseAdapter
extends
BaseAdapter
{
@Override
public
int
getCount()
//返回的是集合的大小
{
return
list.size();
}
@Override
public
Object getItem(
int
position)
//返回的是第一个对象
{
return
list.get(position);
}
@Override
public
long
getItemId(
int
position)
{
return
0
;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent)
{
ViewHolder vh ;
//优化
if
(convertView ==
null
)
{
//因为系统已经默认将R.layout.base_listview_item中的控件添加到了父控件中了,所以写null.
convertView = getLayoutInflater().inflate(R.layout.base_listview_item,
null
);
vh =
new
ViewHolder();
vh.iv = (ImageView) convertView.findViewById(R.id.imageView1);
vh.tv = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(vh);
//将vh存起来,以达到代码优化的效果。
}
else
{
vh = (ViewHolder) convertView.getTag();
}
vh.iv.setImageResource(R.drawable.ic_launcher);
vh.tv.setText(list.get(position));
return
convertView;
}
class
ViewHolder{
TextView tv;
ImageView iv;
}
}
}
|
base_listview.xml中的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
ListView
android:id
=
"@+id/listView1"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_alignParentTop
=
"true"
>
</
ListView
>
</
RelativeLayout
>
|
base_listview_item.xml中的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
RelativeLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
ImageView
android:id
=
"@+id/imageView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
/>
<
TextView
android:id
=
"@+id/textView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentRight
=
"true"
android:layout_alignParentTop
=
"true"
android:text
=
"TextView"
/>
</
RelativeLayout
>
|
源代码下载链接:
本文转自 墨宇hz 51CTO博客,原文链接:http://blog.51cto.com/zzhhz/1631974