Android - 拉动自定义适配器刷新

时间:2013-04-11 13:15:25

标签: android android-listview pull-to-refresh

我正在开发一个Android应用程序,我的ListView有一个自定义适配器。我想使用Pull来刷新库,例如this one。这适用于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);

0 个答案:

没有答案