第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter

1.BaseAdapter

BaseAdapter是Android应用程序中经常用到的基础数据适配器,它的主要用途是将一组数据传到像ListView、Spinner、Gallery及GridView等UI显示组件,它是继承自接口类Adapter。

BaseAdapter实现了ListAdapter和SpinnerAdapter两个接口,当然它也可以直接给ListView和Spinner等UI组件直接提供数据。

利用BaseAdapter构造adapter,需要自定义一个构造类MyAdapter

一、简单的BaseAdapter

MainActivity.java部分代码如下:

private String[] ss=new String[]{"北京1","北京2","北京3","北京4"};

ListView listView=(ListView)findViewById(R.id.listView1);

MyAdapter adapter=new MyAdapter(this, ss);

listView.setAdapter(adapter);

MyAdapter.java部分代码如下:

public class MyAdapter extends BaseAdapter{              //自定义一个构造类MyAdapter

private Context context;

private String[] ss;

public MyAdapter(Context context, String[] ss) {

super();

this.context = context;

this.ss = ss;

}

// getCount 获取数据源元素个数

public int getCount() { return ss.length; }

public Object getItem(int arg0) { return null; }

public long getItemId(int arg0) {return 0; }

//需要构建一个View对象,来展示数据源中的信息

public View getView(int position, View converView, ViewGroup parent){

String str=ss[position];

TextView textView=new TextView(context);

textView.setText(str);

return textView;

}

}

二、复杂的BaseAdapter

1、首先需要在另一个包package里面定义一个class——User.java

(User.java中定义了三个参数,实现这三个参数利用快捷键Alt+Shift+s中的构造函数;而要生成get和set函数,利用快捷键Alt+Shift+s中的Getter和Setter。 )

package com.example.hujun;

public class User {

private String name;

private String phone;

private Integer age;

public User(String name, String phone, Integer age) {

super();

this.name = name;

this.phone = phone;

this.age = age;

}

public String getName() {  returnname; }

public void setName(String name) {  this.name = name; }

public String getPhone() {  returnphone; }

public void setPhone(String phone) {  this.phone = phone; }

public Integer getAge() {  return age; }

public void setAge(Integer age) {  this.age = age; }

}

2、之后在MainActivity.java中,利用自定义的User生成对象user并赋值,将生成的user对象放置在一个list中。

private List<User> list=newArrayList<User>();

for (int i = 0; i < 10; i++) {                   //添加一些简单元素

User user=newUser("hujun"+i, "123", 24+i);

list.add(user);

}

3、然后,为了将user对象放置在布局中,需要自定义一个layout——user_item.xml 分别用三个TextView放置name, phone, age。

4、由于数据源格式改变了,因此需要改变adapter以接受数据源list

package com.example.test_listview2;

public class MyAdapter extends BaseAdapter{

private Context context;

private List<User> list;

public MyAdapter(Context context, List<User> list) {

super();

this.context = context;

this.list = list;

}

//获取数据源元素个数

public int getCount() { return list.size(); }

public Object getItem(int arg0) { return null; }

public long getItemId(int arg0) { return 0; }

//需要构建一个View对象,来展示数据源中的信息

public View getView(int position, View converView, ViewGroup parent){

User user=list.get(position);

LayoutInflater inflater=

(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

ViewGroup group=(ViewGroup) inflater.inflate(R.layout.user_layout,null);

TextView textView1=(TextView) group.findViewById(R.id.textView1);

textView1.setText(user.getName());

TextView textView2=(TextView) group.findViewById(R.id.textView2);

textView2.setText(user.getPhone());

TextView textView3=(TextView) group.findViewById(R.id.textView3);

textView3.setText(String.valueOf(user.getAge()));

return group;

}

}

5、将新的adapter赋予listView

listView.setAdapter(newMyAdapter(this,list));

2.SimpleAdapter

simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。

作用是ArrayList和 ListView的桥梁。这个ArrayList里边的每一项都是一个Map<String,?>类型。

ArrayList当中的每一项Map对象都和ListView里边的每一项进行数据绑定一一对应。

SimpleAdapter(Context context,  List<?extends Map<String, ?>> data,  intresource,  String[] from,  int[] to);

参数:

1,context:上下文。

2,data:基于Map的list。Data里边的每一项都和 ListView里边的每一项对应。Data里边的每一项都是一个Map类型,这个Map类里边包含了ListView每一行需要的数据。

3,resource :就是一个布局layout,可引用系统提供的,也可以自定义。

4,from:这是个名字数组,每个名字是为了在 ArrayList数组的每一个item索引Map<String,Object>的Object用的。即 map 中得key值

5,to:里面是一个TextView数组。这些 TextView是以id的形式来表示的。例如:Android.R.id.text1,这个text1在layout当中是可以索引的。

listitem.xml文件:

<?xmlversion=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:orientation=”horizontal” android:layout_width=”fill_parent”

android:layout_height=”wrap_content”>

<TextView android:id=”@+id/mview1″

android:layout_width=”100px”

android:layout_height=”wrap_content” />

<TextView android:id=”@+id/mview2″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content” />

</LinearLayout>

下面是activity文件的部分代码:

//构造数据部分。

List<Map<String, Object>> data = newArrayList<Map<String,Object>>();

Map<String,Object> item;

item = new HashMap<String,Object>();

item.put(“姓名”,”张三”);       item.put(“性别”,”男”);

data.add(item);

item = new HashMap<String,Object>();

item.put(“姓名”,”李四”);       item.put(“性别”,”女”);

data.add(item);

//构造listview对象。

ListView listview= new ListView(this);

//构造一个适配器。

SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.listitem,new String[]{“姓名”,”性别”},

new int[]{R.id.TextView01, R.id.TextView02});

/*1,第三个参数是说明用的是自定义的布局R.layout.listtiem。

* 2,第四和第五个参数一起理解:把我们添加数据时姓名那一列对应到R.id.TextView01这个TextView中,把性别对应到R.id.TextView02这个 TextView中。

* 如果把from改为new String[]{“姓名”,”姓名”},测试下就会明白。

*/

listview.setAdapter(adapter);

setContentView(listview);

第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter的更多相关文章

  1. 第28讲 UI组件之 ListView和ArrayAdapter

    第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...

  2. 第33讲 UI组件_进度条ProcessBar和消息队列处理器handler

    第33讲UI组件_进度条ProcessBar和消息队列处理器handler 1. 进度条ProcessBar 一个可视化的进度指示器,代表正在执行的耗时任务.可以为用户展示一个进度条,表示正在执行的任 ...

  3. 第30讲 UI组件之 GridView组件

    第30讲 UI组件之 GridView组件 1.网格布局组件GridView GridView是一个ViewGroup(布局控件),可使用表格的方式显示组件,可滚动的控件.一般用于显示多张图片,比如实 ...

  4. 第16讲- UI组件之TextView

    第16讲 UI组件之TextView Android系统所有UI类都是建立在View和ViewGroup这两类的基础上的. 所有View的子类称为widget:所有ViewGroup的子类称为Layo ...

  5. 第34讲 UI组件之 ProgressDialog和Message

    第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...

  6. 第32讲 UI组件之 时间日期控件DatePicker和TimePicker

    第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置,    Time ...

  7. 第31讲 UI组件之 Gallery画廊控件

    第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...

  8. 第27讲 UI组件之 ScrollView与底部动态添加数据

    第27讲 UI组件之 ScrollView与底部动态添加数据 1. ScrollView(滚动视图) ScrollView(滚动视图)是实现滚动的一个控件,只需要将需要滚动的控件添加到ScrollVi ...

  9. 第25讲 UI组件之 AlertDialog 的各种实现

    第25讲 UI组件之AlertDialog 的各种实现 对话框(Dialog)是程序运行中的弹出窗口,例如当用户要删除一个联系方式时,会弹出一个对话框. Android提供了多种对话框:警告对话框(A ...

随机推荐

  1. chrome devtools 实用快捷键

    Ctrl + O:查找资源,非常使用 Ctrl + Shift + C:切换审查元素模式与浏览器窗口模式 Ctrl + Shift + F:在源代码中搜索 Ctrl + G:跳转到指定行

  2. StoryBoard 设置TabBar SelectImage 和tintColor

    如图:StoryBoard 结构是 Tabbar + Navi + ViewController 需求:需要修改TabBar的Image 和SelectImage 设置Image 设置SelectIm ...

  3. 二十六个月Android学习工作总结

    1.客户端的功能逻辑不难,UI界面也不难,但写UI花的时间是写功能逻辑的两倍. 2.写代码前的思考过程非常重要,即使在简单的功能,也需要在本子上把该功能的运行过程写出来. 3.要有自己的知识库,可以是 ...

  4. JavaScript兼容问题汇总[实时更新]

    印象笔记链接地址:点我查看   遇到问题不断更新中--    [转载请注明出处-HTML5自由者] 

  5. Linux安装mysql源码

    1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...

  6. python 下的数据结构与算法---3:python内建数据结构的方法及其时间复杂度

    目录 一:python内部数据类型分类 二:各数据结构 一:python内部数据类型分类 这里有个很重要的东西要先提醒注意一下:原子性数据类型和非原子性数据类型的区别 Python内部数据从某种形式上 ...

  7. C#socket通讯两个最经典错误解决方案

    1.经典错误之 无法访问已释放的对象. 对象名:“System.Net.Sockets.Socket”     (1).问题现场   (2).问题叙述 程序中的某个地方调用到了socket.close ...

  8. XML中 添加或修改时 xmlns="" 怎么删除

    //创建节点时 记得加上  ---> xmldoc.DocumentElement.NamespaceURI XmlElement url = xmldoc.CreateElement(&quo ...

  9. NHibernate之映射文件配置说明(转载3)

    十二.组件(component), 动态组件(dynamic-component) <component>元素把子对象的一些元素与父类对应的表的一些字段映射起来. 然后组件可以定义它们自己 ...

  10. Emoji表情在网页中显示

    最近遇到一个项目,客户手机上发送的表情要在电脑网页中显示,没有找到简便方法,于是有了以下方案. 由于Emoji表情传到后台是“口”,怎么找出接收数据中的表情是关键,各种搜索后,我用下面的正则表达式匹配 ...