使用viewholder重复listview中的项目

时间:2015-01-21 11:28:55

标签: android listview

这个问题已经问过,但我仍然遇到listview重复项目的问题。 请帮助解决这个问题。大多数关于观看者的帖子说它是更顺畅,更快的listview项目加载模式。但是每当我使用它时,我总是遇到这个问题。

public class CustomAdapterWithViewHolder extends ArrayAdapter<Movie> {
    private Activity activity;

    private List<Movie> movieItems;
    private LayoutInflater layoutInflater;
    ImageLoader imageLoader = AppController.getAppController().getImageLoader();

    public CustomAdapterWithViewHolder(Activity activity, List<Movie> movies) {
        super(activity, 0, movies);
        this.activity = activity;
        this.movieItems = movies;
    }

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


        View rowView=convertView;


        ViewHolder viewHolder;
        Movie movie = movieItems.get(position);

       if (rowView == null) {

            LayoutInflater inflater = LayoutInflater.from(getContext());
            rowView = inflater.inflate(R.layout.list_row, null);


            viewHolder=new ViewHolder();
            viewHolder.networkImageView = (NetworkImageView) rowView
                    .findViewById(R.id.thumbnail);
            viewHolder.title = (TextView) rowView.findViewById(R.id.title);
            viewHolder.rating = (TextView) rowView
                    .findViewById(R.id.rating);
            viewHolder.genre = (TextView) rowView.findViewById(R.id.genre);
            viewHolder.year = (TextView) rowView
                    .findViewById(R.id.releaseYear);

            rowView.setTag(viewHolder);
        } 
        else
           viewHolder= (ViewHolder) rowView.getTag();

        viewHolder.networkImageView.setImageUrl(movie.getThumbnailUrl(),
                imageLoader);

        viewHolder.title.setText(movie.getTitle());
        viewHolder.rating.setText(String.valueOf(movie.getRating()));
        String genreStr = "";
        for (String str : movie.getGenre()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        viewHolder.genre.setText(genreStr);

        viewHolder.year.setText(String.valueOf(movie.getYear()));

        return rowView;

    }

    private static class ViewHolder {
        static TextView title;
        static TextView rating;
        static TextView genre;
        static TextView year;
        static NetworkImageView networkImageView;
    }
}

2 个答案:

答案 0 :(得分:0)

您需要在某些方面进行更改。像,

public class CustomAdapterWithViewHolder extends ArrayAdapter<Movie> {
private Activity activity;

private List<Movie> movieItems;
private LayoutInflater layoutInflater;
ImageLoader imageLoader;

public CustomAdapterWithViewHolder(Activity activity, List<Movie> movies) {
    super(activity, 0, movies);
    this.activity = activity;
    this.movieItems = movies;
    layoutInflater = LayoutInflater.from(activity);
    imageLoader = AppController.getAppController().getImageLoader();
}

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

    View rowView=convertView;
    ViewHolder viewHolder;
    Movie movie = movieItems.get(position);

   if (rowView == null) {
        rowView = layoutInflater.inflate(R.layout.list_row, null);
        viewHolder=new ViewHolder();
        viewHolder.networkImageView = (NetworkImageView) rowView
                .findViewById(R.id.thumbnail);
        viewHolder.title = (TextView) rowView.findViewById(R.id.title);
        viewHolder.rating = (TextView) rowView
                .findViewById(R.id.rating);
        viewHolder.genre = (TextView) rowView.findViewById(R.id.genre);
        viewHolder.year = (TextView) rowView
                .findViewById(R.id.releaseYear);

        rowView.setTag(viewHolder);
    } 
    else {
       viewHolder= (ViewHolder) rowView.getTag();
    }

    viewHolder.networkImageView.setImageUrl(movie.getThumbnailUrl(),
            imageLoader);

    viewHolder.title.setText(movie.getTitle());
    viewHolder.rating.setText(String.valueOf(movie.getRating()));
    String genreStr = "";
    for (String str : movie.getGenre()) {
        genreStr += str + ", ";
    }
    genreStr = genreStr.length() > 0 ? genreStr.substring(0,
            genreStr.length() - 2) : genreStr;
    viewHolder.genre.setText(genreStr);

    viewHolder.year.setText(String.valueOf(movie.getYear()));

    return rowView;

}

private static class ViewHolder {
    static TextView title;
    static TextView rating;
    static TextView genre;
    static TextView year;
    static NetworkImageView networkImageView;
}
}

答案 1 :(得分:0)

尝试此类型

public class NewsRowAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
  public ImageLoader imageLoader; 


public NewsRowAdapter(Activity act, int resource, List<Item> arrayList) {
    super(act, resource, arrayList);
    this.activity = act;
    this.row = resource;
    this.items = arrayList;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(row, null);

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

    if ((items == null) || ((position + 1) > items.size()))    
        return view;

    objBean = items.get(position);

    holder.tvRestname = (TextView) view.findViewById(R.id.tvrestname);
    holder.tvAddress = (TextView) view.findViewById(R.id.tvaddress);
    holder.tvDiscount = (TextView) view.findViewById(R.id.tvdiscount);
    holder.tvDis = (TextView) view.findViewById(R.id.tvdis);
    holder.tvCatname = (TextView) view.findViewById(R.id.tvcatname);
    holder.image = (ImageView) view.findViewById(R.id.image);
    view.setTag(holder);


    if (holder.tvRestname != null && null != objBean.getRestname()
            && objBean.getRestname().trim().length() > 0) {

        holder.tvRestname.setText(Html.fromHtml(objBean.getRestname()));
    }
    if (holder.tvAddress != null && null != objBean.getAddress()
            && objBean.getAddress().trim().length() > 0) {

        holder.tvAddress.setText(Html.fromHtml(objBean.getAddress()));
    }

    if (holder.tvDiscount != null && null != objBean.getDiscount()
            && objBean.getDiscount().trim().length() > 0 && !objBean.getDiscount().equals ("0") ) {

        StringBuilder strBuilder = new StringBuilder("discount:");

        //StringBuilder strBuilder = new StringBuilder(Html.fromHtml(objBean.getDiscount()));
        strBuilder.append(Html.fromHtml(objBean.getDiscount()));
        strBuilder.append("%");
      String str = strBuilder.toString();
        holder.tvDiscount.setText(str);
    }

    else {

        StringBuilder strBuilder = new StringBuilder("Offer:");
        strBuilder.append(Html.fromHtml(objBean.getOffer()));
        String str = strBuilder.toString();
        holder.tvDiscount.setText(str);
    }
    if (holder.tvDis != null && null != objBean.getDis()
            && objBean.getDis().trim().length() > 0) {
        String disString =  (String)objBean.getDis();
        String bob = disString.substring(0,4);
        StringBuilder strBuilder = new StringBuilder(bob);

        strBuilder.append("miles");
        String str = strBuilder.toString();
        holder.tvDis.setText(str);
    }


    if (holder.tvCatname != null && null != objBean.getCatname()
            && objBean.getCatname().trim().length() > 0) {
        holder.tvCatname.setText(Html.fromHtml("" + objBean.getCatname()));
    }


if (holder.image != null && null != objBean.getImage()
        && objBean.getImage().trim().length() > 0) {

    }


      String myString =  (String)objBean.getImage();

URL newurl = null;
 try {
        newurl = new URL(myString);

        } catch (MalformedURLException e) {
    e.printStackTrace();            
    } 
   Bitmap mIcon_val = null;
    try {
        mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
    } catch (IOException e) {
            e.printStackTrace();
} 
        holder.image.setImageBitmap(mIcon_val);
return view;
}
private Context getApplicationContext() {
    return null;
}

public class ViewHolder {
public Object Image;
public ImageView image;
public TextView tvCatname;
public TextView tvDis;
public TextView tvDiscount;
public TextView tvRestname;
public TextView tvAddress;
}
}