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
、一行一行的显示对象
①、定义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 ;
}
/**
* 缓存的是被遮住的那一行
*/
@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());
、自定义一个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 ;
}
@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", "");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "");
data.add(info);
info = new HashMap<String, String>();
info.put("name", "wangwu");
info.put("age", "");
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的两种适配器的更多相关文章
- (转载) Android-Spinner的使用以及两种适配器
目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选 程序员8月书讯 项目管理+代码托管+文档协作,开发更流畅 Android-Spinner的使用以及两种适配器 201 ...
- ListView+CheckBox两种解决方式及原因分析
近期在用ListView+CheckBox搞一个item选中的项目,我将CheckBox的focus设置为false,另我大喜的是,CheckBox居然能够选中(窃喜中),这么简单就搞定了,由于数据量 ...
- 【Android自学日记】两种适配器的使用
ArrayAdapter适配器: (1)用于显示基本的文字内容 (2)基本使用过程:新建适配器---创建或加载数据源---适配器加载数据源---视图加载适配器 ArrayAdapter(上下文,当前L ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...
- Android中通过数组资源文件xml与适配器两种方式给ListView列表视图设置数据源
场景 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改 ...
- 一个ListView怎么展示两种样式
private class MyBaseMsgAdapter extends BaseAdapter { //获取数据适配器中条目类型的总数,修改成两种(纯文本,输入+文字) @Override pu ...
- 几种适配器&观察者&ListView之间的那点事
android中的几种适配器&观察者&ListView 1.我们知道Android中的Adapter类是处于ListView和数据源之间的数据总线,它负责为ListView提供数据. ...
- 我的Android进阶之旅------>Android Listview跳到指定条目位置的两种实现方法
前言 今天实现ListView跳转到第一个条目位置时,使用smoothScrollToPosition(int position)方法跳转实现了,但是交互说不需要这样的动画效果,需要直接跳转到第一项, ...
- Android一个ListView列表之中插入两种不同的数据
http://www.cnblogs.com/roucheng/ Android一个ListView列表之中插入两种不同的数据 代码如下: public class ViewHolder{ Butto ...
随机推荐
- P4879 ycz的妹子
思路 让你干啥你就干啥呗 查询第x个妹子就get一下再修改 这里稳一点就维护了三个东西,也许两个也可以 代码 #include <iostream> #include <cstdio ...
- C#中dll附加配置文件
DLL-with-configuration-file带配置文件的dll http://www.codeproject.com/Tips/199441/DLL-with-configuration-f ...
- UVa 11292 勇者斗恶龙
https://vjudge.net/problem/UVA-11292 题意:有n条任意个头的恶龙,你希望雇一些其实把它杀死.一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币 ...
- UVa 1605 联合国大楼
https://vjudge.net/problem/UVA-1605 题意:有n个国家,要求设计一栋楼并为这n个国家划分房间,要求国家的房间必须连通,且每两个国家之间必须有一间房间是相邻的. 思路: ...
- 【NOI2013】小Q的修炼
题目链接:http://uoj.ac/problem/123 又开提答坑啦,要不是一定要讲题谁他妈要这样伤害自己 CASE 1,2 首先可以打一个通用暴力,用于模拟操作过程,对于每一个操作随机一个选择 ...
- [BZOJ]|[Ural] Formula 1-----插头DP入门
1519. Formula 1 Time limit: 1.0 secondMemory limit: 64 MB Background Regardless of the fact, that Vo ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- DEV-C++设置C++11标准
DEV-C++默认的标准是C++98,改成C++11的方法如下: Tools -> Compiler Options -> Setting -> Code Generation -& ...
- c++ 满足条件拷贝,容器扩容(copy_if)
#include <iostream> // cout #include <algorithm> // copy_if, distance #include <vecto ...
- Cglib方法实现动态代理
除了使用JDK方式产生动态代理外,Java还给我们提供了另外一种产生动态代理的方法,那就是使用cglib. cglib是这样实现动态代理的: · ①.针对类来实现代理 · ②对指定目标类产生一个子类 ...