简体   繁体   中英

Android ListView actions repeat in different items

I have a custom layout for a Listview, each row item contains a button that when clicked it shows a small imageview in the item, however the actions i perform in one item, repeats for another item down the list, for example, if i click the button in item 1, the imageview will show up in item 1 and item 10, if i click the button in item 2, the Imageview will show up on item 2 and item 11 and while i scroll it will keep repeating in different items, heres the code for my custom adapter:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        mparent = parent;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.places_item, parent, false);

            holder = new ViewHolder();
            holder.placeimage = (CircularImageView) convertView.findViewById(R.id.locationimage_pilayout);
            holder.addbtn = (TextView) convertView.findViewById(R.id.addbtn_pilayout);
            holder.delbtn = (TextView) convertView.findViewById(R.id.delbtn_pilayout);
            holder.oribtn = (TextView) convertView.findViewById(R.id.oribtn_pilayout);
            holder.placename = (TextView) convertView.findViewById(R.id.locationname_pilayout);
            holder.selected = (ImageView) convertView.findViewById(R.id.selected_pilayout);
            holder.origin = (ImageView) convertView.findViewById(R.id.origin_pilayout);
            holder.swipeLayout = (SwipeRevealLayout) convertView.findViewById(R.id.swipe_pilayout);
            holder.mainLayout = (LinearLayout) convertView.findViewById(R.id.main_pilayout);


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

        final Place item = getItem(position);
        /*
        my code assigning the button click listener and assigning the views
        */
        return convertView;
    }

Am i missing something? im sure this could be a simple fix, but i havent found it yet. any help would be kindly appreciated.

In ListView the individual views for rows, get reused. For example, let's say the window can show up to 10 rows inside the ListView . Now when you scroll down, the 1st view gets out of the window from the top, and a new 11th view comes into the window from the bottom. In order to save memory and CPU power, Android uses the same 1st view for the 11th view. That's the purpose of convertView and the if (convertView == null) {} else {} code.

So the reason why the image is being shown in the 1st item and also in the 11th item, is that they are exactly one view object. To tackle this issue, in the getView() method, you need to reset every attribute of every view and don't rely on the defaults.

So adding a line like the one below, will get rid of the mentioned problem:

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

    // all your code ...

    holder.placeImage.setImageResource(0);  //<-- This clears any previously set image.
    return convertView;
}

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