zoukankan      html  css  js  c++  java
  • ListView遍历每个Item出现NullPointerException的异常

    在使用ListView过程中我们有时候需要遍历取得每个Item项中的一些数据(比如每个Item里面有TextView,需要获取它的文本等等),但是我们在遍历过程中经常会遇到NullPointerException的异常,其实这不过是listview中getCount()和getChildCount()这个两个函数的一些使用上的不同导致的。

    getChildCount():这个函数是获取ListView在当前界面上能够被看到的(不是所有)Item的数目,因为ListView中当Item很多的时候,有些无法显示在屏幕上。

    getCount():这个函数就是可以获取ListView中可见的和不可见的所有Item项数目(附上该函数返回值的官方英文解释)

    The number of items owned by the Adapter associated with this AdapterView. (This is the number of data items, which may be larger than the number of visible views.)

    就只能对当前屏幕可见的Item做遍历,不能到达我们要对所有Item遍历的目的。

    getChildAt():这个函数是用来获得ListView的Item,它能取到的Item的最大值为当前可见的Item数目,比如,当前屏幕只能看到ListView的6个Item,但你调用了getChildAt(6),相当于要获得ListView第七个可见的Item,这个时候就会报出NullPointerException,

    我在这里提供的解决方法就是调用adapter的getView方法,这个方法可以获取ListView的所有Item,mListView.getAdapter().getView(i, null, null);

    具体请参见以下网址

    http://blog.csdn.net/chenzujie/article/details/8680756

  • 相关阅读:
    循环
    if条件语句
    中文字符,全角字符的正则表达式
    常用正则表达式大全!(例如:匹配中文、匹配html)
    PHP中$_SERVER的详细参数与说明
    IIS 伪静态配置(安装ISAPI_Rewrite配置)
    iis php 服务器设置问题
    Apache Rewrite 拟静态配置
    DEDE时间
    DEDE 字符串操作常见问题
  • 原文地址:https://www.cnblogs.com/lyf161/p/3799529.html
Copyright © 2011-2022 走看看