I have an application in which I made an arrylist of images, which in this list of the image has its position, in the onBindViewHolder method
@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 a snippet of my code, this position returns me to the position of the images, all in all there are 21 images in the list, my problem is that in above above if(ChecandoseExisteIMG(nomes[position]))
it checks if the image exists in 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, however to get a very annoying problem for example I downloaded a photo, position 0
Therewasacorrection,butIdidnotdownloadanyphotosbeyondthisone,andinthefolderIcreatedwhenhedownloadedthephotos,thereisonlythephotothatIreallydownloaded,butIcomeacrossit
Theninpositionlist7itgoesandarrowagainbeingthattheimageisnotdownloaded
thisismyvectorofimagenames:
String[]nomes={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21"};
I would like to know how to solve this problem, I do not understand why he is doing this, every 8 numbers he re-arrows what is inside the if CheckingExistImg ....
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.
OnBindViewHolder int position will work correctly tb because I tested it with a TOAST that shows how it is being updated, I tested it and it's the Toast ' s from 0 to 20 which shows that the 21 positions are working correctly, who has how to help thank you ..
If you need more, I'll edit the post ...
Updated - Methods that checks 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);
}
}