在前面的两篇文章中,我们讲到了关于ArrayAdapter的使用。用ArrayAdapter来在ListView中展示数据是很不错的,但是很多时候,我们的ListView中,可不只是展示文字,我们还想展示图片呢。
可能有些朋友刚才会问,第二篇不是已经可以展示图片了吗?是的呀,但是它就只能展示我们在xml中定义给它的那一张啊。
而究其原因,其实是因为我们传给它的数据源就只有字符串,没有传给图片给它,而事实上,我们也只能传文字给它,因为它用的是TextView嘛。
所以,光用ArrayAdapter显然不够丰富多彩,生活呀,总得有点不一样吧。
接下来我们就来看看在Android中关于SimpleAdapter的使用吧。
SimpleAdapter
SimpleAdapter其实一点也不simple的,因为我们需要自己去定义每个listitem的布局,如下:
1
2
3
4
5
6
|
<!--?xml version=
"1.0"
encoding=
"utf-8"
?-->
<relativelayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:padding=
"5dip"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<imageview android:id=
"@+id/imageView1"
android:layout_width=
"80dip"
android:layout_height=
"60dip"
android:layout_alignparenttop=
"true"
android:layout_alignparentleft=
"true"
android:contentdescription=
"testing"
>
<textview android:id=
"@+id/tvTitle"
android:layout_width=
"80dip"
android:layout_height=
"wrap_content"
android:layout_torightof=
"@+id/imageView1"
android:layout_aligntop=
"@+id/imageView1"
>
<textview android:id=
"@+id/tvContent"
android:layout_width=
"80dip"
android:layout_height=
"wrap_content"
android:layout_below=
"@+id/tvTitle"
android:layout_torightof=
"@+id/imageView1"
android:layout_alignbottom=
"@+id/imageView1"
>
</textview></textview></imageview></relativelayout>
|
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
|
List<map<string, object=
""
>> simpleList =
new
ArrayList<map<string, object=
""
>>();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initSimpleList();
ListView listView = (ListView)findViewById(R.id.listView1);
SimpleAdapter adapter =
new
SimpleAdapter(
this
, simpleList, R.layout.simpleadapter,
new
String[] {
"title"
,
"content"
,
"drawable"
},
new
int
[] {R.id.tvTitle,R.id.tvContent,R.id.imageView1});
listView.setAdapter(adapter);
}
private
void
initSimpleList(){
String[] titles = {
"title1"
,
"title2"
,
"title3"
,
"title4"
,
"title5"
,
"title6"
};
String[] contents = {
"content1"
,
"content2"
,
"content3"
,
"content4"
,
"content5"
,
"content6"
};
int
[] drawableIds = {R.drawable.computer,R.drawable.excel,R.drawable.game,
R.drawable.gc,R.drawable.network,R.drawable.pdf};
Map<string, object=
""
> map;
for
(
int
i=
0
;i<
6
;i++){
map =
new
HashMap<string, object=
""
>();
map.put(
"title"
, titles[i]);
map.put(
"content"
, contents[i]);
map.put(
"drawable"
, drawableIds[i]);
simpleList.add(map);
}
}</string,></string,></map<string,></map<string,>
|
从上面的代码中,我们可以看到,跟ArrayAdapter类似,也要先创建一个SimpleAdapter,其构造函数如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* Constructor
*
* @param context The context where the View associated with this SimpleAdapter is running
* @param data A List of Maps. Each entry in the List corresponds to one row in the list. The
* Maps contain the data for each row, and should include all the entries specified in
* "from"
* @param resource Resource identifier of a view layout that defines the views for this list
* item. The layout file should include at least those named views defined in "to"
* @param from A list of column names that will be added to the Map associated with each
* item.
* @param to The views that should display column in the "from" parameter. These should all be
* TextViews. The first N views in this list are given the values of the first N columns
* in the from parameter.
*/
public
SimpleAdapter(Context context, List<!--?
extends
Map<String, ?-->> data,
int
resource, String[] from,
int
[] to) {
mData = data;
mResource = mDropDownResource = resource;
mFrom = from;
mTo = to;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
|
虽然说SimpleAdapter一点也不simple,但是如果我们理解了,用起来是不是也是挺简单的呢?
大家如果对ArrayAdapter有兴趣的话,可以看看前面关于ArrayAdapter的文章: Android中关于Adapter的使用(上)ArrayAdapter
Android中关于Adapter的使用(再上)ArrayAdapter
http://www.2cto.com/kf/201402/278676.html