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 ...
随机推荐
- linux下连接无线网出现nl80211: Could not configure driver mode nl80211: deinit ifname=wlan1 disabled_11b_rates=0 wlan1: Failed to initialize driver interface
一.背景1.1 jello@jello:~$ lsb_release -aNo LSB modules are available.Distributor ID: UbuntuDescripti ...
- 'curl' is not recognized as an internal or external command
使用everything搜索本地的curl.exe发现如下 官网查看最新版本https://curl.haxx.se/windows/ 2019-03-06 最新版本7.64.0 curl-7.64. ...
- C# 给某个方法设定执行超时时间
ManualResetEvent.WaitOne 方法 https://msdn.microsoft.com/en-us/library/system.threading.manualreseteve ...
- 51nod 1183 编辑距离
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183. 题意不再赘述. 分析:大概和LCS差不多的吧 但是我用LCS ...
- 获取String类型汉字乱码,如何进行编码
本文为博主原创,未经允许不得转载: 在解析properties文件中的汉字时,在java代码中解析得到的是一个乱码字符,形如图下: 导致乱码原因:由于在jdk中,默认为gbk编码方式进行编码盒接收的, ...
- C# asp.net 比较两个时间的差求天数
string str1 = "2017-2-13 23:59:59"; string str2 = "2017-2-14 0:00:01"; DateTime ...
- json字符串在javascript和java代码中的表示方式
最近在使用js写json形式的字符串的时候,当我把json形式的字符串放到函数JSON.parse()的时候,总是报错Uncaught SyntaxError: Unexpected token ' ...
- python网络编程之TCP通信实例
一. server.py import socket host="localhost" port= s=socket.socket(socket.AF_INET,socket.SO ...
- python 元组查找元素返回索引
#create a tuple tuplex = tuple("index tuple") print(tuplex) #get index of the first item w ...
- python 函数赋值
⾸先我们来理解下Python中的函数 def hi(name="yasoob"): return "hi " + name print(hi()) # outp ...