码迷,mamicode.com
首页 > 其他好文 > 详细

ListView遍历每个Item出现NullPointerException的异常处理(转)

时间:2014-10-13 22:31:47      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   os   使用   ar   for   sp   数据   

在使用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.)

现在看一个例子

package com.jackchan.listviewexception; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
     
    private ListView mListView; 
    private Button btn; 
    private String[] items = new String[]{"Item1","Item2","Item3","Item4","Item5","Item6", 
            "Item7","Item8","Item9","Item10","Item11","Item12","Item13","Item14","Item15","Item16","Item17","Item18" 
            }; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mListView = (ListView)findViewById(R.id.listview); 
        btn = (Button)findViewById(R.id.btn); 
        mListView.setAdapter(new ListViewAdapter()); 
        btn.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
            //  for(int i = 0; i < mListView.getChildCount(); i++) 获取屏幕上可显示的Item数目  
                for(int i = 0; i < mListView.getCount(); i++)//获取ListView的所有Item数目  
                { 
                //    LinearLayout linearlayout = (LinearLayout)mListView.getChildAt(i);  
                    LinearLayout linearlayout = (LinearLayout)mListView.getAdapter().getView(i, null, null); 
                    TextView textview = (TextView)linearlayout.getChildAt(0); 
                    Log.d("text", textview.getText().toString()); 
                } 
            } 
        }); 
    } 
 
    public class ViewHolder{ 
        TextView textview; 
    } 
     
    private class ListViewAdapter extends BaseAdapter{ 
         
         
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub  
            return items.length; 
        } 
 
        @Override 
        public Object getItem(int position) { 
            // TODO Auto-generated method stub  
            return null; 
        } 
 
        @Override 
        public long getItemId(int position) { 
            // TODO Auto-generated method stub  
            return 0; 
        } 
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            ViewHolder viewHolder = new ViewHolder(); 
            if(convertView == null){ 
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.listviewitem, null); 
                viewHolder.textview = (TextView)convertView.findViewById(R.id.textview); 
                convertView.setTag(viewHolder); 
            } 
            else{ 
                viewHolder = (ViewHolder)convertView.getTag(); 
            } 
            viewHolder.textview.setText(items[position].toString()); 
            return convertView; 
        } 
         
    } 

package com.jackchan.listviewexception;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 private ListView mListView;
 private Button btn;
 private String[] items = new String[]{"Item1","Item2","Item3","Item4","Item5","Item6",
   "Item7","Item8","Item9","Item10","Item11","Item12","Item13","Item14","Item15","Item16","Item17","Item18"
   };
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListView = (ListView)findViewById(R.id.listview);
        btn = (Button)findViewById(R.id.btn);
        mListView.setAdapter(new ListViewAdapter());
        btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
   // for(int i = 0; i < mListView.getChildCount(); i++) 获取屏幕上可显示的Item数目
    for(int i = 0; i < mListView.getCount(); i++)//获取ListView的所有Item数目
          {
    //    LinearLayout linearlayout = (LinearLayout)mListView.getChildAt(i);
           LinearLayout linearlayout = (LinearLayout)mListView.getAdapter().getView(i, null, null);
           TextView textview = (TextView)linearlayout.getChildAt(0);
           Log.d("text", textview.getText().toString());
          }
   }
  });
    }

    public class ViewHolder{
     TextView textview;
    }
   
    private class ListViewAdapter extends BaseAdapter{
     
     
  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return items.length;
  }

  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   ViewHolder viewHolder = new ViewHolder();
   if(convertView == null){
    convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.listviewitem, null);
    viewHolder.textview = (TextView)convertView.findViewById(R.id.textview);
    convertView.setTag(viewHolder);
   }
   else{
    viewHolder = (ViewHolder)convertView.getTag();
   }
   viewHolder.textview.setText(items[position].toString());
   return convertView;
  }
     
    }
}

重点看Button的OnClickListener事件,当我们使用for(int i = 0; i < mListView.getChildCount(); i++)

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

在for循环里还有一个被我注释起来的方法getChildAt();

getChildAt():这个函数是用来获得ListView的Item,它能取到的Item的最大值为当前可见的Item数目,比如,当前屏幕只能看到ListView的6个Item,但你调用了getChildAt(6),相当于要获得ListView第七个可见的Item,这个时候就会报出NullPointerException ,我在这里提供的解决方法就是调用adapter的getView方法,这个方法可以获取ListView的所有Item,mListView.getAdapter().getView(i, null, null);

ListView在android开发中是经常用到的组件,除非只做展示功能,否则一般建议自定义Adapter,这样可以省去很多麻烦。最后附上两个.xml的代码

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    xmlns:tools="http://schemas.android.com/tools
    android:layout_width="match_parent" 
    android:layout_height="match_parent"  
    android:orientation="vertical"> 
    <Button 
        android:id="@+id/btn" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="changetext" 
        /> 
    <ListView  
        android:id="@+id/listview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        /> 
</LinearLayout> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="changetext"
        />
 <ListView
     android:id="@+id/listview"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     />
</LinearLayout>

listviewitem.xml

 <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 
    <TextView  
        android:id="@+id/textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textSize="20sp" 
        /> 
 
</LinearLayout> 

ListView遍历每个Item出现NullPointerException的异常处理(转)

标签:android   http   io   os   使用   ar   for   sp   数据   

原文地址:http://www.cnblogs.com/qingchen1984/p/4022889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!