0

我正在开发一个 Android 应用程序,并且我有一个自定义适配器用于我的ListView. 我想使用 Pull 来刷新库,例如这个. 这适用于 ArrayAdapter,但不适用于自定义适配器。

如何让我的拉动刷新使用自定义适配器?

这是我的适配器代码:

public class AdvertListAdapter extends BaseAdapter {
private AdvertListActivity activity;
private int currentFirstVisibleItem=0;
private int currentLastVisibleItem=8;

public AdvertListAdapter(AdvertListActivity activity) {
    this.activity = activity;

}

public void setFirstVisibleItem(int currentFirstVisibleItem) {
    this.currentFirstVisibleItem=currentFirstVisibleItem;
}

public void setLastVisibleItem(int currentLastVisibleItem) {
    this.currentLastVisibleItem=currentLastVisibleItem;
}

@Override
public int getCount() {
    return activity.getAdvertList().size();
}

@Override
public Advert getItem(int pos) {
    return activity.getAdvertList().get(pos);
}

@Override
public long getItemId(int pos) {
    return activity.getAdvertList().get(pos).getId();
}

private class ViewHolder {
    public ImageView image;
    public TextView title;
    public TextView price;
    public TextView size;
    public TextView city;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder view;
    LayoutInflater inflator = activity.getLayoutInflater();

    if(convertView == null) {
        view = new ViewHolder();
        convertView = inflator.inflate(R.layout.listview_advertlist, null);

        view.image = (ImageView) convertView.findViewById(R.id.advertImage);
        view.title = (TextView) convertView.findViewById(R.id.advertTitleText);
        view.price = (TextView) convertView.findViewById(R.id.advertPriceText);
        view.size = (TextView) convertView.findViewById(R.id.advertSizeText);
        view.city = (TextView) convertView.findViewById(R.id.advertCityText);

        convertView.setTag(view);
    }else {
        view = (ViewHolder) convertView.getTag();
    }

    Advert advert = activity.getAdvertList().get(position);

    view.title.setText(advert.getTitle());
    view.price.setText(String.valueOf(advert.getPrice()+"€"));
    view.size.setText("Taille: "+advert.getSize());
    view.city.setText(advert.getCity());

    if(position <= currentFirstVisibleItem || position >= currentLastVisibleItem) {
        view.image.setImageResource(R.drawable.defaultadvertimage);
        ServiceHelper.loadAsyncImage(view.image, activity.getApplicationContext(), String.valueOf(advert.getId()+"-mini"), "advert");
    }

    return convertView;

}

}

这是我的活动代码:

list = (PullToRefreshListView) findViewById(R.id.advertList);

    advertList = getAdvertList();

    ((PullToRefreshListView) list).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.
            new GetDataTask().execute();
        }
    });

    list.setAdapter(adapter);
4

0 回答 0