使用自定义适配器在listview中有时不会调用getview()方法。

时间:2014-06-05 09:47:08

标签: android listview android-listview getview

我使用自定义适配器使用图片网址显示图片。有时它工作正常,但有时getview()没有被调用。我找不到代码中的任何错误。

public class GridViewAdapter  extends ArrayAdapter<ListViewVariables> {

private final Activity context;
private final List<ListViewVariables> list;
DisplayImageOptions doption=null;
private ImageLoadingListener animateFirstListener =null;
ImageLoader imageLoader= null;

public GridViewAdapter(Activity context, List<ListViewVariables> list) {
    super(context, R.layout.list_layout, list);
    this.context = context;
    this.list = list;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public ListViewVariables getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

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

static class ViewHolder {

    protected ImageView image;

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.list_layout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.image = (ImageView) view.findViewById(R.id.image);

        view.setTag(viewHolder);
    } else {
        view = convertView;
    }
    ViewHolder holder = (ViewHolder) view.getTag();
  imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    imageLoader.displayImage(list.get(position).getImageUrl(), holder.image,doption,animateFirstListener);
    return view;

}

用于显示图像我正在使用图像加载器和调用函数如下:

list = EventManagementResuableClass.getResponseFromServer(
                    EventManagenmentConstant.responseStatus_object,
                    EventActivity.this, jsonObj);
            adapter = new GridViewAdapter(EventActivity.this, list);
            adapter.clear();
            adapter.notifyDataSetChanged();
            listView.setAdapter(adapter);

通常我会得到清单。但是某些时候getview()似乎没有被执行。什么是确切的问题?

1 个答案:

答案 0 :(得分:0)

为什么你有以下几行?

adapter.clear();
adapter.notifyDataSetChanged();

尝试没有那些线......