Search code examples
androidgridviewbaseadapter

Update GridView Items through Adapter causing conflict between items


Am using GridView with BaseAdapter and when pressing download on an gridview item the download button will disappear and Circular Progress Drawable will appear and start progressing but the problem here is that when and item is downloading and i scroll the list the Circular Progress is appearing on all the items which is not the true status of these items and here is a picture enter image description here

the first one is the true one and the second is just messed up and the circular progress shouldnot appear.

Here is my code og getView() method of the adapter:

public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.book_item_to_download, parent, false);
            convertView.setFocusable(true);
            convertView.setClickable(true);

            holderGridView = new BooksViewHolder();

            holderGridView.BookImage = (ImageView) convertView.findViewById(R.id.bookBackground);
            holderGridView.BookName = (TextView) convertView.findViewById(R.id.bookName);
            holderGridView.BookItemWraper = (LinearLayout) convertView.findViewById(R.id.book_item_wraper);
            holderGridView.BookDownload=(ImageView) convertView.findViewById(R.id.download);
            holderGridView.BookProgress=(CircularProgressBar) convertView.findViewById(R.id.demo_mpc);

            Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Isra-Thin.ttf");
            holderGridView.BookName.setTypeface(custom_font);
            convertView.setTag(holderGridView);
        } else {
            holderGridView = (BooksViewHolder) convertView.getTag();
        }

        holderGridView.BookItemWraper.setLayoutParams(new android.widget.AbsListView.LayoutParams((int) Math.round(ScreenWidth / 2.1), (int) Math.round(ScreenHeight / 2.5)));
        holderGridView.BookImage.setImageDrawable(getResources().getDrawable(R.drawable.book_background));
        holderGridView.BookName.setText(bookCovers.get(position).getBookTitle());


        if (bookCovers.get(position).getBookImagePathOnDisk() != null && bookCovers.get(position).getBookImagePathOnDisk().contains("file://")) {
            Picasso.with(mContext).load(bookCovers.get(position).getBookImagePathOnDisk()).into(holderGridView.BookImage);
        } else if (bookCovers.get(position).getBookImagePathOnDisk()!= null) {
            Picasso.with(mContext).load(new File(bookCovers.get(position).getBookImagePathOnDisk())).into(holderGridView.BookImage);
        }

        if(convertView.getId()==bookCovers.get(position).getBookID())
        {
            if(bookCovers.get(position).isDownloaded)
            {
                holderGridView.BookDownload.setVisibility(View.GONE);
                holderGridView.BookProgress.setVisibility(View.GONE);
                holderGridView.BookItemWraper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for(int i=0;i<app.bis.size();++i)
                        {
                            if(app.bis.get(i).identifier.equals(bookCovers.get(position).getBookID()+""))
                            {
                                openBookViewer(app.bis.get(i));
                                break;
                            }
                        }
                    }
                });
            }
            else if(bookCovers.get(position).isDownloading)
            {
                holderGridView.BookDownload.setVisibility(View.GONE);
                holderGridView.BookProgress.setVisibility(View.VISIBLE);
                holderGridView.BookProgress.setProgress(bookCovers.get(position).downloadProgress);
                holderGridView.BookItemWraper.setOnClickListener(null);
            }
            else
            {
                holderGridView.BookDownload.setVisibility(View.VISIBLE);

                holderGridView.BookItemWraper.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent openDetails = new Intent(getActivity(), WaitingBookDetails_Activity.class);
                        openDetails.putExtra(Constants.BOOK_COVER, Parcels.wrap(bookCovers.get(position)));
                        startActivity(openDetails);
                    }
                });

                holderGridView.BookDownload.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new downloadEPUB_Task(bookCovers.get(position),position).execute();
                    }
                });
            }
        }



        return  convertView;
    }

thanks in advance


Solution

  • The issue is because of recycling of views. Add

     holderGridView.BookProgress.setVisibility(View.GONE);
    

    before your outer IF condition

    if(convertView.getId()==bookCovers.get(position).getBookID())
            {
    ..
    }
    

    and set

    holderGridView.BookProgress.setVisibility(View.VSIBLE); 
    

    in those if or else blocks where you want it to show.