Android: Change image for a particular item in listview

陌路散爱 提交于 2019-12-01 05:16:02
RobinHood

Issue with getview Method which keep recreating whenever you scroll your view, to handle exact position you have to play with setTag & getTag,check below few stackvoerflow answers to understand setTag & getTag:

Button in ListView using ArrayAdapter

Getting radio button value from custom list in android

and even store downloaded state into one booleanarray like below:

int boxState[];

within adapter constructor, set zero initially:

for (int i = 0; i < getData.size(); i++) {
    boxState[i] = 0;

    }

within adapter getview method:

holder.imgDownload.setTag(position);

Now you click on download button set value as 1 (Inside onclick of button):

pos = (Integer) v.getTag();
boxState[pos]=1;

At last when you scroll your view check condition into following way(put below code inside getview method):

if (boxState[position] == 0) {
            holder.imgDownload.setImageDrawable(ctx.getResources()
                    .getDrawable(R.drawable.ic_dloaded)); //which aren't downloaded
        } else {
             holder.imgDownload.setImageDrawable(ctx.getResources()
                    .getDrawable(R.drawable.ic_dload)); // which are downloaded.
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!