Android之 ListView(1)
ListView是Android中最常用的控件之一。
当有太多数据需要显示的时候,ListView就派上用场了。它允许用户通过滑动手指的方式,将数据滑入滑出界面。
一、最简单的ListView实现
1、修改布局文件。我们在activity_main.xml中加入空间ListView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lkb.listviewtest.MainActivity"> <ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView> </LinearLayout>
2、回到MainActivity.java中。首先我们创建需要显示的数据。
private String[] fruit = {"apple","watermelon","mango","strawberry","pineapple","jujube","banana",
"apple","watermelon","mango","strawberry","pineapple","jujube","banana"};
3、最后在代码中实现往ListView中添加数据。数组中的数据是无法直接传递给ListView的,我们需要借助适配器。
常用的适配器有ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。这里使用ArrayAdapter。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,fruit);
这里我们获取了一个ArrayAdapter对象。ArrayAdapter的构造函数有六个。这个对应的是
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull T[] objects) {
this(context, resource, 0, Arrays.asList(objects));
}
android.R.layout.simple_list_item_1是Android内置的一个布局文件的id。这里我们使用它作为ListView中子项的布局。
最后通过ListView的setAdapter方法,将构建好的适配器对象传递进去,这样ListView和数据之间的关联就建立完成了。
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
通过上面三步,我们就实现了ListView。
Android之 ListView(1)的更多相关文章
- Android—万能ListView适配器
ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...
- Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)
昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...
- Android之ListView性能优化——一行代码绑定数据——万能适配器
如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有 ...
- Android中ListView的几种常见的优化方法
Android中的ListView应该算是布局中几种最常用的组件之一了,使用也十分方便,下面将介绍ListView几种比较常见的优化方法: 首先我们给出一个没有任何优化的Listview的Adapte ...
- android——自定义listView
都知道微信主机面 有个界面会一行一一行的聊天记录,那个效果就可以用listview来实现(当然这只是其中的一种) listView是一种比较常见的组件它用来展示列的view,它是根据数据的长度来显示数 ...
- 分享个刚写好的 android 的 ListView 动态加载类,功能全而代码少。
(转载声明出处:http://www.cnblogs.com/linguanh/) 简介: 该ListView 实现动态加载数据,为了方便用户充分地自定义自己的数据源.点击事件,等核心操作, ...
- Android UI ListView的使用
一.ListView的理解 1.什么ListView? 一种用来显示多个可滑动项(Item)列表的的ViewGroup 需要使用Adapter将集合数据和每一个Item所对应的布局动态适配到Li ...
- Android中Listview点击item不变颜色以及设置listselector 无效
Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...
- Android中ListView动态加载数据
1. 引言: 为了提高ListView的效率和应用程序的性能,在Android应用程序中不应该一次性加载ListView所要显示的全部信息,而是采取分批加载策略,随着用户的滑动,动态的从后台加载所需的 ...
- Android 自定义ListView
本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView. 系统显示列表(L ...
随机推荐
- 2017-2018-2 20179207 《网络攻防技术》第十三周作业 python3实现SM234算法
国密算法SM234 的python3实现 国家标准 GM/T 0002-2012 <SM4分组密码算法> GM/T 0003.1-2012 <SM2椭圆曲线公钥密码算法 第1部分:总 ...
- 3.redis设计与实现--字典
1.包括三个结构体:字典结构体+哈希表结构体+哈希节点结构体 2.如何解决哈希冲突? 答:redis使用的是链地址法来解决哈希冲突的,每个链表节点有一个next指针,最新加入的节点会放在链表的头部. ...
- Linux改变用户shell的类型
命令: 改变usr01的类型 # usermod -s /bin/csh usr01
- Network File System
Network File System 2014-12-31 #system 接着上一篇博客Distributed Systems 分布式系统来扯淡,之前的博客一再在写文件系统,这次继续,只不过是分布 ...
- Java学习遇到的问题
一. Java中泛型如何比较大小,继承Comparable类,然后实现其唯一的方法compareTo(): public class GenericClass<E extends Compara ...
- Let's Encrypt 免费通配 https 签名证书 安装方法2 ,安卓签名无法认证!
Let's Encrypt 免费通配 https 签名证书 安装方法 按照上文 配置完毕后你会发现 在pc浏览器中正常访问,在手机浏览器中无法认证 你只需要安装一个或多个中级证书 1.查看Nginx ...
- 转:国内优秀npm镜像推荐及使用
原文:http://riny.net/2014/cnpm/ npm全称Node Package Manager,是node.js的模块依赖管理工具.由于npm的源在国外,所以国内用户使用起来各种不方便 ...
- mysql-connector-python取二进制字节时报错UnicodeDecodeError:'utf-8' codec can't decode byte 0xb0 in position 0
在储存用户密码时,我使用了hmac算法对用户密码加密,加密出来的hash值是一个二进制字节串,我把这个字节串存到mysql的password字段,password字段的数据类型是varbinary. ...
- Linux增加swap文件
起因 在阿里云搞了台ECS,但是内存就1个G,操作总是悲剧的卡卡卡,于是就想着增加一点交换文件来缓解一下. 快速添加交换文件 step 1. 生成文件 先填充一个大文件,等会儿当做交换文件用: dd ...
- [LeetCode] Intersection of Two Linked Lists 两链表是否相交
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...