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 ...
随机推荐
- [luogu2119]魔法阵 NOIP2016T4
很好的一道数学推导题 45分做法 $O(N^4)$暴力枚举四个材料 55分做法 从第一个约束条件可得到所有可行答案都是单调递增的,所以可以排序一遍,减少枚举量,可以拿到55分 100分做法 首先可以发 ...
- HDU 5963 朋友(找规律博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=5963 题意: 思路: 我们可以先只考虑单链,自己试几种案例就可以发现规律,只有与根相连的边为1时,只需要奇数次操 ...
- HDU 1757 A Simple Math Problem(矩阵快速幂模板)
题意:题意很简单,不多说了. 思路: |f(10) | |a0 a1 a2 ...a8 a9| |f(9)|| f(9) | | 1 0 0 ... 0 ...
- python后端工程师 数据爬虫
大数据挖掘分析平台和产品的建设. 工作职责: 独立完成软件系统代码的设计与实现: 根据需求完成设计,代码编写,调试,测试和维护: 使用Python 语言对后台业务逻辑进行开发以及维护: 能根据实际需求 ...
- mui --- 怎么获取百度地图定位功能
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- Linux Shell学习笔记(一)
Shell,见名知意,就是一个作为用户与Linux OS间接口的程序,允许用户向OS输入需要执行的命令.Shell众多,这里只介绍Bash. 0)实验的Shell版本 显示shell版本: /bin/ ...
- JavaScript页面跳转的一些实现方法
第一种 <script language=”javascript” type=”text/javascript”> window.location.href=”login.jsp?back ...
- python 散列表查找
class HashTable: def __init__(self, size): self.elem = [None for i in range(size)] self.count = size ...
- 03_ExeZZ.cpp
1.静态加载 DLL : #pragma comment(lib, "DllZZ.lib") __declspec(dllimport) void __stdcall AA(); ...
- 一个纯净的webpack4+angular5脚手架
该篇主要是结合刚发布不久的webpack4,搭建一个非cli的angular5的脚手架demo,主要分为以下几个方面阐述下脚手架结构: # 脚手架基础架构(根据angular5的新规范) /** * ...