首页 技术 正文
技术 2022年11月14日
0 收藏 983 点赞 2,973 浏览 1553 个字

Adapter可以视作控件与数据之间的桥梁

对ListView做自由布局和填充需要使用到Adapter,这里我们采用SimpleAdapter。

简单来说:

1.定义一个ListItem,其数据结构是一个元素为HashMap的ArrayList。

2.填充ListItem

3.使用一个SimpleAdapter将ListItem与Item.xml关联起来

4.将ListView与SimpleAdapter关联起来

逻辑关系用VISO表示如下:

ListView与SimpleAdapter

下面是代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {    ListView list;    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView)findViewById(R.id.list1); ArrayList<HashMap<String , Object>> listItem = new ArrayList<HashMap<String, Object>>();
for(int i=0;i<10;i++){
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("ItemImage",R.drawable.icon);
map.put("ItemTitle",i+"row");
listItem.add(map);
}
SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,R.layout.item,
new String[]{"ItemImage","ItemTitle","ItemText"},
new int[]{R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
list.setAdapter(mSimpleAdapter);
}
}

Item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<ImageView
android:layout_alignParentLeft="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ItemImage"/>
<TextView
android:id="@+id/ItemTitle"
android:layout_toRightOf="@+id/ItemImage"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:textSize="20sp"/>
<TextView
android:id="@+id/ItemText" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_below="@+id/ItemTitle"/>
</RelativeLayout>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,499
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,912
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,746
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,503
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,305