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 ...
随机推荐
- Python3基础 random 配合while输出10个随机整数
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- windows10下使用source insight出现"source insight program editor已停止工作"的问题
一.背景 1.1 OS 版本 windows 10 1.2 source insight版本 source insight 3.50.0034 二.解决方案 删除"我的文档"下面的source ins ...
- IntelliJ-IDEA和Git、GitHub、Gitlab的使用
一.基本入门 1.IntelliJ-IDEA预装的版本控制介绍 我们来看IntelliJ-IDEA的版本控制设置区域 打开File>Settings>Version Control 可以 ...
- ajax中的contentType使用
本文为博主原创,未经允许不得转载: 最近在修改部分项目功能的时候,遇到一个问题.局部刷新某页面的功能是由ajax实现的,但当我进行局部刷新的时候,页面并没有刷新和响应, 在后台的代码中打了断点也并没有 ...
- JS post 数组道后台
$("#aSave").click(function () { if ($("#TaskName").val() == "") { aler ...
- ubuntu14.04上 nginx启动停止
sudo service nginx stop 停止 sudo nginx 启动
- java 使用正则判断是不是一个数字
public class Numeric { public static void main(String[] args) { String string = "-1234.15" ...
- Python day20正则表达式和re方法
元字符6个函数以及几个元字符1.'.'通配符2.'^'以什么开头3.'$'以什么结尾4.'*'紧挨着的字符0~∞次5.'+'紧挨着的字符1~∞次6.'?'紧挨的字符0次或1次7.'{}' {0,}== ...
- 自定义 Maven 的 repositories
有时,应用中需要一些比较新的依赖,而这些依赖并没有正式发布,还是处于milestone或者是snapshot阶段,并不能从中央仓库或者镜像站上下载到.此时,就需要 自定义Maven的repositor ...
- 算法笔记--priority_queue
算法笔记 priority_queue<int>que;//默认大顶堆 或者写作:priority_queue<int,vector<int>,less<int&g ...