在ListView中使用SimpleAdapter
来源:互联网 发布:spu sku 数据库设计 编辑:程序博客网 时间:2024/06/06 06:53
在上一篇博客中介绍了在ListView中使用 ArrayAdapter,使用ArrayAdapter存在一个弊端,只能处理一种数据,在上一篇博客中,只能处理好友的姓名,不能处理好友的图片,为了既可以处理好友的姓名又可以处理好友的图片,ListView中提供了一个SimpleAdapter,使用SimpleAdapter既可以处理好友的姓名,也可以处理好友的图片,实现的效果如下
首先使用Android Studio创建一个Android工程
1、在drawable文件夹添加十张图片,作为人物的头像
修改activity_main.xml文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="example.administrator.arrayadaptedemo.MainActivity"> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ListView></RelativeLayout>
3、新建一个item_listview.xml文件作为ListView中的条目所在的布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/iv_photo" android:layout_width="80dp" android:layout_height="80dp" android:src="@drawable/man1"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="20sp" android:text="小志"/></LinearLayout>
4、新建一个Person类,用于处理好友的信息
package example.administrator.arrayadaptedemo;public class Person { private String name; private int imageId; public Person(String name, int imageId) { this.name = name; this.imageId = imageId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; }}
package example.administrator.arrayadaptedemo;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建集合 List<Person> personList = new ArrayList<Person>(); //创建person对象 Person person1 = new Person("刘得意", R.drawable.man1); Person person2 = new Person("王锐", R.drawable.man2); Person person3 = new Person("何煜中", R.drawable.man3); Person person4 = new Person("王磊", R.drawable.man4); Person person5 = new Person("冯松", R.drawable.man5); Person person6 = new Person("裴培", R.drawable.man6); Person person7 = new Person("马骁", R.drawable.man7); Person person8 = new Person("马婧", R.drawable.man8); Person person9 = new Person("周俊升", R.drawable.man9); Person person10 = new Person("贺祺", R.drawable.man10); //将Person对象添加到集合中 personList.add(person1); personList.add(person2); personList.add(person3); personList.add(person4); personList.add(person5); personList.add(person6); personList.add(person7); personList.add(person8); personList.add(person9); personList.add(person10); //获得布局文件上的ListView控件 ListView lv = (ListView) findViewById(R.id.lv); //集合中每个元素都包含ListView条目需要的所有数据 // 该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据 List< Map<String, Object> > data = new ArrayList< Map<String,Object> >(); for(Person p : personList) { Map<String, Object> map = new HashMap <String, Object>(); map.put("name", p.getName()); map.put("photo", p.getImageId()); data.add(map); } //给ListView设置适配器 //第一个参数:上下文 //第二个参数:用于设置好友的头像和名字 //第三个参数:布局文件的资源id //第四个参数:设置朋友的头像和名字属性 //第五个参数:设置朋友的头像和名字的资源id lv.setAdapter(new SimpleAdapter( this, data, R.layout.item_listview, new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name})); }}
0 0
- 在ListView中使用SimpleAdapter
- 在ListView中使用SimpleAdapter
- 在ListView中如何使用SimpleAdapter
- Android中ListView之SimpleAdapter的使用
- 使用SimpleAdapter创建ListView
- Android ListView使用SimpleAdapter
- ListView-使用SimpleAdapter创建
- 使用SimpleAdapter填充ListView
- 使用SimpleAdapter创建ListView
- ListView -- SimpleAdapter的使用
- 填充ListView(使用SimpleAdapter)
- 使用 SimpleAdapter 显示listview
- 使用SimpleAdapter创建ListView
- 使用SimpleAdapter创建ListView
- ListView使用SimpleAdapter适配器
- Android中使用ListView显示列表之SimpleAdapter的使用
- 通过SimpleAdapter直接在ListView中显示图片
- Android中动态生成ListView及SimpleAdapter的使用
- 自定义MySeekBar,动态设置能否拖动
- 解决存到数据库里中文乱码问题
- 认识adb手记
- BPFP系列:优化电池使用时间:监控电池电量和充电状态
- 持续集成之Jenkins插件使用(二)- 邮件通知模板化
- 在ListView中使用SimpleAdapter
- Hi3520D UART2和UART3是如何加载到内核的
- 利用vc实现数据表格导出到CSV文件
- 自定义view之超级课程表页面的实现
- Android中高德地图与百度地图坐标转换
- redis+mybatis+spring
- windows坏境下NATS服务搭建与测试
- const与#define
- Effective STL Item 6: Be alert for C++’s most vexing parse.