使用方法1

显示简单的文本

  1. 在layout文件中像加入普通控件一样在layout文件中引入ListView

    <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    </ListView>
  2. 数组中的数据要通过适配器来导入ListView,Android有很多的适配器,常用的有ArrayAdapter,它通过泛型指定要适配的数据类型,然后在构造函数中把要适配的数据传入即可。

//data is a String Array
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity_simplelist.this, android.R.layout.simple_list_item_1, data);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);

使用方法2

显示定制的界面

  1. 定义子项的构成

    public class Fruit {
    private String name;
    private int imageId;
    public Fruit(String name, int imageId) {
    this.name = name;
    this.imageId = imageId;
    }
    public String getName() {
    return name;
    }
    public int getImageId() {
    return imageId;
    }
    }
  2. 然后需要为子项写好布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dip" /> </LinearLayout>

3, 接下来还要自定义适配器

3.1 自定义适配器要写构造函数,有三个参数分别是上下文,ListView子项布局的id和数据

 public FruitAdapter(Context context, int textViewResourceId,
List<Fruit> objects) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}

3.2 还需要重写getView方法,这个方法在滚动屏幕的时候被调用

  3.2.1 在getView方法中首先调用getItem取得当前子项的实例

Fruit fruit = getItem(position);

  3.2.2 然后调用LayoutInflater来为这个子项加载传入的布局

view = LayoutInflater.from(getContext()).inflate(resourceId, null);

  3.2.3 接着调用findViewById找到布局中的各个控件的id,然后调用它们的set函数设置图片或文字

fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());

如何提升ListView的效率

方法1: 利用缓存的布局 getView()有一个convertView参数,这个参数用于将之前加载好的布局进行缓存,当这个参数不为null时,直接使用而不是重新加载布局。

if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}

方法2:对控件实例进行缓存 当加载布局时,将控件保存到ViewHolder中;当不需要重新加载布局时,直接从ViewHolder中取出。

if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder = new ViewHolder();
viewHolder.fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
viewHolder.fruitName = (TextView) view.findViewById(R.id.fruit_name);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
 

Android中ListView的用法的更多相关文章

  1. Android中Listview点击item不变颜色以及设置listselector 无效

    Android中Listview点击item不变颜色以及设置listselector 无效 这是同一个问题,Listview中点击item是会变颜色的,因为listview设置了默认的listsele ...

  2. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

  3. android中ListView控件&&onItemClick事件中获取listView传递的数据

    http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...

  4. android中ListView点击和里边按钮点击不能同时生效问题解决

    今天遇到一个问题:android中ListView点击和里边button点击不能同时生效问题解决. 原因是: listView 在开始绘制的时候,系统首先调用getCount()函数,根据他的返回值得 ...

  5. android 中uri.parse()用法

    android 中uri.parse()用法 1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = ...

  6. Android中ListView无法点击

    Android中ListView无法点击 转自:http://xqjay19910131-yahoo-cn.iteye.com/blog/1319502   问题描述: ListView中Item加入 ...

  7. Android中Selector的用法(改变ListView和Button的默认背景)

    Android中的Selector的用法 http://blog.csdn.net/shakespeare001/article/details/7788400#comments Android中的S ...

  8. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  9. Android中ListView实现图文并列并且自定义分割线(完善仿微信APP)

    昨天的(今天凌晨)的博文<Android中Fragment和ViewPager那点事儿>中,我们通过使用Fragment和ViewPager模仿实现了微信的布局框架.今天我们来通过使用Li ...

随机推荐

  1. PHP的PDO

    PDO中包含三个预定一类:PDO.PODStatement和PDOException. 1.PDO类 PDO类代表一个PHP和数据库之间的连接,PDO类所拥有的方法如下: PDO:构造器,构建一个新的 ...

  2. SQL-Server使用点滴(二)

    二,对象的建立和使用 1,了解MSSql的[系统表] 对于SQL-Server中的所有对象,包括数据库,数据表,记录,字段,触发器,索引,数据类型等元素,均有对应的系统表记性记录.系统表是禁止直接删改 ...

  3. EasyUI、Struts2、Hibernate、spring 框架整合

    经历了四个月的学习,中间过程曲折离奇,好在坚持下来了,也到了最后框架的整合中间过程也只有自己能体会了. 接下来开始说一下整合中的问题和技巧: 1,  jar包导入 c3p0(2个).jdbc(1个). ...

  4. Greenplum第三方工具链接

    在master节点的$MASTER_DATA_DIRECTORY/pg_hba.conf中添加新客户端服务器信息 #add host     all         gpadmin         0 ...

  5. Struts2配置Result(Struts2_result)

    一.概要 二.常用四种类型的配置 Struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!D ...

  6. springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传

    总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...

  7. 统计学习中感知机的C++代码

    感知机是古老的统计学习方法,主要应用于二类线性可分数据,策略是在给定的超平面上对误差点进行纠正,从而保证所有的点都是正确可分的. 用到的方法是随机梯度下降法,由于是线性可分的,可保证最终在有限步内收敛 ...

  8. join / left join / right join

    1 可以自己join自己 SELECT * FROM table_1 t1join table_1 ton t.target_id = t1.target_id 2 join要双方都满足才可以出现结果 ...

  9. RDLC中添加参数,用来显示报表中数据集之外的信息。

    我添加了两个参数,首先后台: ReportParameter rp = ,,).ToString()); ReportParameter rp1 = new ReportParameter(" ...

  10. null

    期末考备考最后三天. 加油,把这学期学的知识给它搞透了.