Android: Change image for a particular item in listview

后端 未结 1 2010
日久生厌
日久生厌 2021-01-12 18:18

\"screen

In the above image, there is a list view which contains list of items that the user can downloa

相关标签:
1条回答
  • 2021-01-12 19:08

    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.
            }
    
    0 讨论(0)
提交回复
热议问题