第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. (转)iOS消息推送机制中pem文件的生成

    转自:http://2015.iteye.com/blog/1567777 以前写了一篇文章:iOS消息推送机制的实现,这篇文章中生成的是p12文件,但是php是用的pem文件,生成的方法和p12文件 ...

  2. CopyOnWriteArrayList理解与理解

     CopyOnWriteArrayList,因何而存在? ArrayList的一个线程安全的变体,其所有可变操作(add.set 等)都是通过对底层数组进行一次新的复制来实现的,代价昂贵. CopyO ...

  3. 最新的四款国外VPN,免费稳定,可以看国外网站的

    tyle="margin-top:20px; margin-right:0px; margin-bottom:0px; margin-left:0px; font-family:Arial; ...

  4. [Angular 2] implements OnInit, OnDestory for fetching data from server

    Link: https://angular.io/docs/js/latest/api/core/OnInit-interface.html, https://www.youtube.com/watc ...

  5. debian linux 中如何查看软件包是否已经安装和如何安装、卸载软件

    练习 1 方案:确定软件包是否安装 如果您不确定某个软件包是否已经安装,可以使用 dpkg 的 -l (L的小写) 选项: $ dpkg -l zsh No packages found matchi ...

  6. Qt之加密算法

          在写这篇文章之前,我曾反复思量关于加密的叫法是否准确,更为严格来说,应该是密码散列-将数据(如中英文字母.特殊字符)通过复杂的算法转换为另一种固定长度的值.   QCryptographi ...

  7. linux/unix运行级别

    在SYSTEM V 风格的UNIX系统中,系统被分为不同的运行级别,这和BSD分支的UNIX有所不同,常用的为0~6七个级别:0关机 1单用户 2不带网络的多用户 3带网络的多用户 4保留,用户可以自 ...

  8. 网页CSS1

    样式的属性 1,背景与前景 background-color: //背景的颜色 background-image:url //背景图片 background-attachment:fixed; //背 ...

  9. JAVA--好友界面面板

    package GongYou; //package windows.best_demo; import java.awt.*; import javax.swing.*; import java.u ...

  10. nginx log日志分割

    @echo offrem 备份并根据时间重命名错误日志文件set "cmdstr=move E:\nginx\logs\error.log E:\nginx\logs\error%date: ...