ListView的几种形式
一、 ArrayAdapter
ListView listView = (ListView) findViewById(R.id.list_view);//ListView的参数为id
listView.setAdapter(new ArrayAdapter<Person>(this, android.R.layout.simple_list_item_1, list));//对象是Person,第一个参数是context,第二个是指代要显示的模版,最后一个是要显示的数据,list为person类的ArrayList集合。
二、 BaseAdapter
1、一行一行的显示对象
①、定义MyAdapter来继承BaseAdapter
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();//list为person对象的List
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
/**
* 缓存的是被遮住的那一行
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textview = null;
if (null != convertView) {
textview = (TextView) convertView;
} else {
textview = new TextView(MainActivity.this);
}
textview.setText(list.get(position).toString());
return textview;
}
}
②、设置适配器
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setAdapter(new MyAdapter());
2、自定义一个xml,加入到ListView中再一行一行显示
①、定义自己的要显示的一行的内容布局文件----list_item.xml
②、定义MyAdapter来继承BaseAdapter
class MyAdapter extends BaseAdapter
{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//布局转换器 作用就是讲一个布局转换为一个对象
LayoutInflater flater = MainActivity1.this.getLayoutInflater();
View view = flater.inflate(R.layout.list_item, null); //真正将一个布局文件转为一个对象
//在一个特定的对象上去查找一个ID所对应的组件
TextView text_name = (TextView) view.findViewById(R.id.list_view_name);
TextView text_age = (TextView) view.findViewById(R.id.list_view_age);
Person person = list.get(position);
text_name.setText(person.getName());
text_age.setText(String.valueOf(person.getAge()));
return view;
}
}
③、设置适配器
ListView listview = (ListView) findViewById(R.id.list_view);
listview.setAdapter(new MyAdapter());
三、SimpleAdapter,显示的一行内容里面包含多行数据
①、定义自己的要显示的一行中要显示的多行的布局文件----list_item.xml
②、设置适配器(代码的意思是要显示的多行xml中是一行name,一行age);
ListView listview = (ListView) findViewById(R.id.list_view);
List<Map<String, String>> data = new ArrayList<Map<String, String>>();
Map<String, String> info = new HashMap<String, String>();
info.put("name", "zs");
info.put("age", "20");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "111");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "111");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "111");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "111");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "111");
data.add(info);
SimpleAdapter simple = new SimpleAdapter(this, data,
R.layout.list_item, new String[] { "name", "age" }, new int[] {
R.id.list_view_name, R.id.list_view_age });
listview.setAdapter(simple);
ListView的几种形式的更多相关文章
- 代替jquery $.post 跨域提交数据的N种形式
跨域的N种形式: 1.直接用jquery中$.getJSON进行跨域提交 优点:有返回值,可直接跨域: 缺点:数据量小: 提交方式:仅get (无$.postJSON) $.getJSON(" ...
- C++:一般情况下,设计函数的形参只需要两种形式
C++:一般情况下,设计函数的形参只需要两种形式.一,是引用形参,例如 void function (int &p_para):二,是常量引用形参,例如 void function(const ...
- jquery插件的两种形式
这里总结一下jquery插件的两种形式,一种是通过字面量的形式组织代码,另一种是通过构造函数的方式.下面就两种形式来分析俩个例子. 例子1: ;(function ($,window,document ...
- javascript面向对象系列第三篇——实现继承的3种形式
× 目录 [1]原型继承 [2]伪类继承 [3]组合继承 前面的话 学习如何创建对象是理解面向对象编程的第一步,第二步是理解继承.本文是javascript面向对象系列第三篇——实现继承的3种形式 [ ...
- 移动端App广告常见的10种形式
什么是App广告? App广告,或称In-App广告,是指智能手机和平板电脑这类移动设备中第三方应用程序内置广告,属于移动广告的子类别. App广告兴起得益于其载体—App的风行.平板电脑和大屏触 ...
- SQL 关于apply的两种形式cross apply 和 outer apply(转)
转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...
- Struts2中Action接收参数的四种形式
1.Struts2的Action接收参数的三种形式. a. 使用Action的属性接收(直接在action中利用get方法来接收参数): login.js ...
- Node.js-提供了四种形式的定时器
Node.js提供了四种形式的定时器 global.setTimeout(); //一次性定时器 global.setInterval(); //周期性定时器 global.nextTick(); / ...
- 参数传递的四种形式----- URL,超链接,js,form表单
什么时候用GET, 查,删, 什么时候用POST,增,改 (特列:登陆用Post,因为不能让用户名和密码显示在URL上) 4种get传参方式 <html xmlns="http:/ ...
随机推荐
- OC基础(27)
单例设计模式 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !import ...
- Grunt 之 RequireJS
RequireJs 提供了一个打包工具 r.js,可以将相关的模块打包为一个文件.相关说明:http://www.requirejs.org/docs/optimization.html 将相关的脚本 ...
- Adobe AIR and Flex - 实现堆栈容器
1.需求描述: 在对云平台的监控中,我们经常需要在一张图上可视化的描述集群下的物理机所虚拟的虚拟机使用情况,以及超卖情况.那么传统的chart图就不满足我们的需求了,那么要实现这样一个定制化的char ...
- matlab 椭圆方程拟合
拟合椭圆首先要知道各个点的坐标,然和带入如下公式: x = [59 136 58 137 57 137 56 137 55 138 54 139 53 140 52 141 51 142 51 143 ...
- Oracle 存储过程包
create or replace package body cuttoship_lots is procedure prod_run(p_w_day date) as begin delete cu ...
- Google Protocol Buffer 协议
1. Protocol Buffers 简介 Protocol Buffers (ProtocolBuffer/ protobuf )是Google公司开发的一种数据描述语言,类似于XML能够将结构化 ...
- 理解Linux启动过程
传统的Linux系统启动过程主要由著名的init进程(也被称为SysV init启动系统)处理,而基于init的启动系统被认为有效率不足的问题,systemd是Linux系统机器的另一种启动方式,宣称 ...
- Java 对字符反转操作。
//把一段字符串反转后大小写互换位置 public class test_demo { public static void main(String[] args)throws Exception { ...
- DEDECMS自动编号(序号)autoindex属性
让织梦dedecms autoindex,itemindex 从0到1开始的办法! 1 2 3 [field:global name=autoindex runphp="yes"] ...
- 法线贴图——Normal Mapping
对于不曾学过.用过法线贴图的人来说,提到法线贴图,经常会提到的问题是什么是法线贴图?法线贴图用于解决什么问题?法线贴图的原理是什么?本文将就这三个问题阐述本人的一些见解,各位不喜勿喷!!! 谈到法线贴 ...