我用ListView显示数据时,自定义了一个适配器(extends ArrayAdapter),然后重写了getView方法,现在出现一个问题,就是这个getView()方法被重复调用了,比如我的_data中有两条数据,但是System.out.println(house.toString());这句却被执行了4次甚至更多,请问各位这是神马情况?自定义适配器如下:package com.hb.puppet.utils;import java.util.List;import com.hb.puppet.activity.MetaData;
import com.hb.puppet.activity.R;
import com.hb.puppet.entity.House;import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;public class CustomCollectAdapter extends ArrayAdapter<House> {
private static final String CLASSTAG = CustomCollectAdapter.class
.getSimpleName();
private ListView _listView;
private int _resource;
private List<House> _data;
private LayoutInflater _inflater;
private AsyncLoadImageTask _asyncloader; public CustomCollectAdapter(Context context, ListView listView,
List<House> data) {

super(context, 0, data);

_resource = R.layout.list_item_collect;
_data = data;
_inflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
_asyncloader = new AsyncLoadImageTask();
_listView = listView;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
CollectListItemViewCache viewCache = null;
//
final int index = position;
//
final ViewGroup p = parent; if (view != null) {
viewCache = (CollectListItemViewCache) view.getTag();
} else {
view = _inflater.inflate(_resource,null);
viewCache = new CollectListItemViewCache(view);
view.setTag(viewCache);
}
// 房源数据
House house = _data.get(position);
System.out.println(house.toString());

if (house != null) {
String imageUrl = MetaData.HOST + house.getTitlePic();
ImageView imageView = viewCache.getImageView();
imageView.setTag(imageUrl);
                        //异步加载图片
new AsyncImageLoaderTask().execute(imageUrl,imageView); // 房源标题
TextView houseTitle = viewCache.getHouseTitle();
houseTitle.setText(house.getTitle());
// 房源单价
TextView housePrice = viewCache.getHousePrice();
housePrice.setText(house.getSinglePrice() + "元/㎡");
// 房源面积
TextView houseArea = viewCache.getHouseArea();
houseArea.setText(house.getArea() + "㎡");
// 房源户型
TextView houseUnit = viewCache.getHouseUnit();
houseUnit.setText(house.getUnits());
// 单项删除收藏房源
Button delButton = viewCache.getDelButton();
delButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("sssssss");
_listView.removeViewAt(index);
}
});
}
return view;
}
}异步加载图片:package com.hb.puppet.utils;import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;public class AsyncImageLoaderTask extends AsyncTask<Object, Object, Bitmap>{
private String classTag = AsyncImageLoaderTask.class.getSimpleName();
private ImageView _view;
private HashMap<String, SoftReference<Bitmap>> imageCache;

public AsyncImageLoaderTask() {
imageCache = new HashMap<String, SoftReference<Bitmap>>();
}

@Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
String url = (String)params[0];
_view = (ImageView)params[1];
if(_view == null){
Log.e(classTag,classTag + " value of _view is not null");
return null;
}

if(url != null){
//if current url is int imageCache,get this drawable by it,then return the drawable
if(imageCache.containsKey(url)){
SoftReference<Bitmap> mapSoft = imageCache.get(url);
bitmap = mapSoft.get();
if(bitmap != null){
return bitmap;
}
}
//
URL fromUrl = null;
try {
fromUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection)fromUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream input = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(input);
input.close();
} catch (MalformedURLException e) {
Log.e(classTag, classTag + " Method doInBackground -- " +e.getMessage());
} catch (IOException e) {
Log.e(classTag, classTag + " Method doInBackground -- " +e.getMessage());
} catch(Exception e){
Log.e(classTag, classTag + " Method doInBackground -- " +e.getMessage());
}

imageCache.put(url, new SoftReference<Bitmap>(bitmap));
}

return bitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
if(bitmap != null){
System.out.println("onPostExecute");
this._view.setImageBitmap(bitmap);
this._view = null;
}
}
}

解决方案 »

  1.   

    你每显示一条ListView信息,将会调用一次ArrayAdapte,没错啊 是要重复使用getView(). 
      

  2.   

    阿军,看下这个
    此问题在于,ListView没有取到实际的高度,无法确定取多少View来填充ListView,也就是运行getView()的具体运行次数。
    解决方法给ListView设置固定高度
      

  3.   

    http://hi.baidu.com/blogofivan/blog/item/e572728206260ec39123d925.html这里有原因的详细解释
    另外, http://www.eoeandroid.com/thread-70518-1-1.html  这里建议干脆把 View 存起来。
      

  4.   

    改成android:layout_height="fill_parent"就好了,很顺畅了,内伤了1天问题终于解决了~~~
      

  5.   

    我全部设置为android:layout_height="fill_parent",还是被执行了2次~!
    另外的一个界面却被执行了5次~~
     
    后来我采用了给listView设定android:layout_height="300dip" ,的确只循环调用一次,可是却又带来另外一个问题了~~
       不同的手机屏幕尺寸,如何做到自适应呢~~???
     这真是个很麻烦的问题呢~~~