Problem with vector position

Asked

Viewed 76 times

0

I have an application in which I made an image arrylist, which in this image list has its position, in the method onBindViewHolder

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
        holder.imageView.setImageBitmap(images.get(position));

    // clicar nas imagens e setar
    holder.imageView.setTag(position);
    holder.Baixar.setTag(position);
    holder.aplicar.setTag(position);


    if (ChecandoseExisteIMG(nomes[position])){
         holder.Baixar.setText("Baixado.");
         holder.Baixar.setEnabled(false);
    }


    // botão baixar
    holder.Baixar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int clickPosition = (int) v.getTag();

            if (haveNetworkConnection()){
                downloadFile(ImagensVT[clickPosition],nomes[clickPosition]);
                Toast.makeText(context, "Imagem " + (clickPosition+1)+" Baixando!",Toast.LENGTH_SHORT).show();
            }else {

                Toast.makeText(context, "Não há internet por favor conecte-se!",Toast.LENGTH_SHORT).show();
            }


        }
    }); ...... restante do código      }
    });

This is an excerpt from my code,this position returns me the position of the images, in all there are 21 images in the list, my problem is that in the if above if(ChecandoseExisteIMG(nomes[position])) it checks if the image exists in the internal memory if the image is in the memory of the device already downloaded it will set the button as downloaded and not to be clickable anymore, but to get a very boring problem for example downloaded a photo, from position 0

inserir a descrição da imagem aqui

ai worked correcting but I did not download any photos other than this, and in the folder that I created when he downloads the photos only has the photo that I actually downloaded, but I come across this inserir a descrição da imagem aqui

after in the 7 position list it goes and arrow again being that the image is not downloaded

this is my vector of image names:

String [] nomes = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21"};

I wonder how to solve this problem, I do not understand why he is doing this, every 8 numbers he again arrow what is inside if Checandoseexisteimg....

This String vector is just a vector to name the images, in which case it takes the name according to the position to check if the image is in the internal memory..

I already looked if it could be something in the check method and I saw nothing there, the int position of the Onbindviewholder works correctly tb because I tested it with a TOAST that shows how it is being updated, I took the test and it the Toast s from 0 to 20 that shows that the 21 positions are working correctly, who have to help thank you..

If you need anything else I edit the post...

Updated - Methods that check for image

 public static boolean ChecandoseExisteIMG(String ImagenNome)
{
    Bitmap b = null ;
    File file = getImage("/"+ImagenNome+".jpg");
    String path = file.getAbsolutePath();

    if (path != null)
        b = BitmapFactory.decodeFile(path);


    if(b == null ||  b.equals(""))
    {
        return false ;
    }
    return true ;
}

// pegando imagem caso tenha
public static File getImage(String imagename) {

    File mediaImage = null;
    try {
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root);
        if (!myDir.exists())
            return null;

        mediaImage = new File(myDir.getPath() + "/PixelWallpapers"+imagename);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return mediaImage;
}

Viewholder

public class ViewHolder extends RecyclerView.ViewHolder {

    ImageView imageView;
    Button Baixar;
    Button aplicar;


    public ViewHolder(View itemView) {
        super(itemView);

        aplicar=(Button) itemView.findViewById(R.id.aplicar);
        Baixar = (Button) itemView.findViewById(R.id.baixarimg);
        imageView = (ImageView) itemView.findViewById(R.id.my_imageview);

    }
}
  • Can you add the method code that checks if the image exists? Probably the error is there

  • I updated @leofontes

  • You could post your Viewholder?

  • @Added Cleitonoliveira

1 answer

0


I managed to resolve, I do not know very well what happened before I had tried this way :

if (ChecandoseExisteIMG(nomes[position])){
     holder.Baixar.setText("Baixado.");
     holder.Baixar.setEnabled(false);
}

Now I did so, I could have done it from the beginning rs, but at least I broke my head and I was able to solve now my list it just arrow what really has in the code, the problem is not in the methods but with something related to the position of the Buttons...

holder.Baixar.setEnabled(!Existe(nomes[position]));

Might not have been the best solution, but at least disable I got :)

and also optimized was using two methods atoa to check image clear that I took advantage of them otherwise because I need to get what was already saved, for the check function I did so:

public boolean Existe(String nome){

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root);

    boolean exists = new File(myDir.getPath() + "/PixelWallpapers" +"/"+nome+".jpg").exists();

    return exists;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.