简体   繁体   中英

Android - ListView Cells are adding more ImageViews when scrolling ListView

I have a layout that defines the way the Cells look in my ListView, but when i scroll the only part of the Cell that changes when i scroll are the ImageViews which sit inside the LinearLayout View.

What should happen is that there should only be 5 stars displayed inside this LinearLayout but when i scroll i get loads of these star images inside my LinearLayout when only 5 stars should be displayed.

This is my Custom Array Adapter:

public class ThreadArrayAdapter extends ArrayAdapter<ThreadObj> {


        Context context;
        int layoutResourceId;
        public ArrayList<ThreadObj> data = null;

        public ThreadArrayAdapter(Context context, int layoutResourceId, ArrayList<ThreadObj> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        //WeatherHolder holder = null;
        ThreadHolder t = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            //TextView tv = (TextView)row.findViewById(R.id.threadTitle);
            //t = new ThreadObj(tv.getText().toString());

            t = new ThreadHolder();
            //holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            t.threadUsernameTV = (TextView)row.findViewById(R.id.threadUsername);
            t.threadDateTV = (TextView)row.findViewById(R.id.threadDate);
            t.threadRatingIV = (ImageView)row.findViewById(R.id.threadRating);
            t.threadTitleTV = (TextView)row.findViewById(R.id.threadTitle);
            t.threadCategoryTV = (TextView)row.findViewById(R.id.threadCategory );
            t.threadSubCategoryTV = (TextView)row.findViewById(R.id.threadSubCategory);
            t.threadCountryTV = (TextView)row.findViewById(R.id.threadCountry);
            t.threadStarLL = (LinearLayout)row.findViewById(R.id.threadStarContainer);
            t.threadFlagIV = (ImageView)row.findViewById(R.id.threadFlag);

            row.setTag(t);
        }
        else
        {
            t = (ThreadHolder)row.getTag();
        }

        ThreadObj thread = data.get(position);
        t.threadUsernameTV.setText(thread.firstname + " " + thread.lastname);
        t.threadDateTV.setText(thread.date);
        t.threadTitleTV.setText(thread.title);
        t.threadCategoryTV.setText(thread.category);
        t.threadSubCategoryTV.setText(thread.subCategory);
        t.threadCountryTV.setText(thread.country);

        if(thread.ageRating == 4){
            t.threadRatingIV.setImageResource(R.drawable.agerating17);
        }
        else if(thread.ageRating == 3){
            t.threadRatingIV.setImageResource(R.drawable.agerating12);
        }
        else if(thread.ageRating == 2){
            t.threadRatingIV.setImageResource(R.drawable.agerating7);
        }
        else{
            t.threadRatingIV.setImageResource(R.drawable.ageratingpg);
        }
        //holder.imgIcon.setImageResource(weather.icon);

        for(int i = 0; i < 5; i++){

            ImageView star1 = new ImageView(this.context);
            star1.setImageResource(R.drawable.star1);

            ImageView star2 = new ImageView(this.context);
            star2.setImageResource(R.drawable.star2);

            if(i < thread.rating){
            t.threadStarLL.addView(star2);
            }else{
            t.threadStarLL.addView(star1);
            }

        }

        if(thread.flags > 0){
            t.threadFlagIV.setImageResource(R.drawable.flag2);
        }else{
            t.threadFlagIV.setImageResource(R.drawable.flag1);
        }


        return row;
        }

        static class ThreadHolder
        {
        //ImageView imgIcon;
        TextView threadUsernameTV;
        TextView threadDateTV;
        ImageView threadRatingIV;
        TextView threadTitleTV;
        TextView threadCategoryTV;
        TextView threadSubCategoryTV;
        TextView threadCountryTV;
        LinearLayout threadStarLL;
        ImageView threadFlagIV;
        }


    }

You're always creating new ImageView s and adding them to the t.threadStarLL layout -- even when re-using an existing view. One way to fix this would be to clear out the threadStarLL layout before adding the new stars.

Try adding this line before the for loop where you create the stars:

t.threadStarLL.removeAllViews();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM