第29讲 UI组件之 ListView与 BaseAdapter,SimpleAdapter
第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的更多相关文章
- 第28讲 UI组件之 ListView和ArrayAdapter
第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...
- 第33讲 UI组件_进度条ProcessBar和消息队列处理器handler
第33讲UI组件_进度条ProcessBar和消息队列处理器handler 1. 进度条ProcessBar 一个可视化的进度指示器,代表正在执行的耗时任务.可以为用户展示一个进度条,表示正在执行的任 ...
- 第30讲 UI组件之 GridView组件
第30讲 UI组件之 GridView组件 1.网格布局组件GridView GridView是一个ViewGroup(布局控件),可使用表格的方式显示组件,可滚动的控件.一般用于显示多张图片,比如实 ...
- 第16讲- UI组件之TextView
第16讲 UI组件之TextView Android系统所有UI类都是建立在View和ViewGroup这两类的基础上的. 所有View的子类称为widget:所有ViewGroup的子类称为Layo ...
- 第34讲 UI组件之 ProgressDialog和Message
第34讲UI组件之 ProgressDialog和Message 1.进度对话框 ProgressDialog <1>简介 ProgressDialog是AlertDialog类的一个扩展 ...
- 第32讲 UI组件之 时间日期控件DatePicker和TimePicker
第32讲 UI组件之 时间日期控件DatePicker和TimePicker 在Android中,时间日期控件相对来说还是比较丰富的.其中, DatePicker用来实现日期输入设置, Time ...
- 第31讲 UI组件之 Gallery画廊控件
第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...
- 第27讲 UI组件之 ScrollView与底部动态添加数据
第27讲 UI组件之 ScrollView与底部动态添加数据 1. ScrollView(滚动视图) ScrollView(滚动视图)是实现滚动的一个控件,只需要将需要滚动的控件添加到ScrollVi ...
- 第25讲 UI组件之 AlertDialog 的各种实现
第25讲 UI组件之AlertDialog 的各种实现 对话框(Dialog)是程序运行中的弹出窗口,例如当用户要删除一个联系方式时,会弹出一个对话框. Android提供了多种对话框:警告对话框(A ...
随机推荐
- 菜鸟玩云计算之十六:Ubuntu14.04上创建的虚拟机迁移到RHEL6.4
菜鸟玩云计算之十六:Ubuntu14.04上创建的RHEL6.4虚拟机迁移到RHEL6.4主机上 RHEL6.4 Server作为虚拟机的HOST,执行以下的命令检查配置和安装相关软件: # egre ...
- gcc和g++编译c或者c++文件碰到的问题
gcc和g++都是GNU(组织)的一个编译器. 误区一:gcc只能编译c代码,g++只能编译c++代码 两者都可以,但是请注意: 1.后缀为.c的,gcc把它当作是C ...
- Bitmap的一些操作
1.截取 Bitmap 的部分区域 mBitmap = Bitmap.createBitmap(bmp, 100, 100, 120, 120); 这句代码从 bmp 的 (100,100) 处截取 ...
- c语言数组小练习
//查找数组中最大的值: #include<stdio.h> int main01() { , , , , , , , , , ,,}; ]; int i; ;i < ]);i++) ...
- tmux environment keep
Shell 下 tmux 不能保持 PATH 变量,解决方法如下: 添加如下配置信息到 ~/.tmux.conf 中即可. set-option -ga update-environment PATH ...
- vb.net转换为C#方法
第一个方法是上面有人说过的,用VB.NET TO C#的在线转换工具.既然你说不好用,那么可以这样:你把VB.NET代码编译了,然后用工具反编译(IL spy,reflector等)成C#代码.
- 触发器内insert,delete,update判断执行不同的内容
create trigger tr_aon afor insert,update,delere asbegin IF EXISTS(SELECT 1 FROM inserted) AND NOT EX ...
- jQuery事件与事件对象
事件是脚本编程的灵魂,本篇来介绍jQuery中的事件处理及事件对象. 事件与事件对象 首先,我们来看一下经常使用的添加事件的方式: <input type="button" ...
- ZOJ 1633
迭代 每个数对应前面的一个数 #include<stdio.h> #include<iostream> using namespace std; #define max 88 ...
- OSG调试信息显示
调试信息显示 OSG 可以将各式各样的调试信息输出到std:cout.这在开发OSG 程序时十分有用,你可以借此观察OSG 的执行的各种操作.环境变量OSG_NOTIFY_LEVEL用于控制OSG调试 ...