Update image of a listview update incorrect index

0

I need to update the image of each position of my listview , the problem is that so far it updates the image, but of the wrong index, in the screenshot below there are 2 buttons, for 2 images respectively, the current problem is that the second image is updated by pressing the 2 buttons, I mean, if I press the first button, the second image is updated and if you pressed the second button, the second image is updated again, hence the first button, do not change the first image, then I leave the code you use:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewclientes, null);
            holder = new CustomAdapterPublicidad.ViewHolder();

            holder.publicidadboton = (Button) convertView
                    .findViewById(R.id.publicidadboton);
            holder.publicidadimagen = (ImageView) convertView
                    .findViewById(R.id.publicidadimagen);


            final RowItemPublicidad row_pos = rowItems.get(position);

            holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());
            holder.publicidadboton.setText(row_pos.getBoton());

            convertView.setTag(holder);
        } else {
            holder = (CustomAdapterPublicidad.ViewHolder) convertView.getTag();
        }

        holder.publicidadboton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(position==0){
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda1").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();

                }else if(position==1) {
                    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                    Bitmap myBitmap = QRCode.from(user.getUid() + "tienda2").withHint(EncodeHintType.MARGIN, 0).bitmap();
                    holder.publicidadimagen.setImageBitmap(myBitmap);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

}

    
asked by zhet 17.04.2017 в 22:35
source

1 answer

-1

Remember that setImageResource () receives an integer as a parameter that is the id of a resource within the project.

If what you are trying to load is a url you should change to:

  //   holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());    
    URL url = new URL(row_pos.getFotoPublicidad());
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    holder.publicidadimagen.setImageBitmap(bmp);

Within the Adapter , specifically the getView() method, the view is being created or reused and the only thing that you must change are its properties, you must take out the property change in the views outside the if.

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {



        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.listviewclientes, null);
            holder = new CustomAdapterPublicidad.ViewHolder();

            holder.publicidadboton = (Button) convertView
                    .findViewById(R.id.publicidadboton);
            holder.publicidadimagen = (ImageView) convertView
                    .findViewById(R.id.publicidadimagen);


           /* RowItemPublicidad row_pos = rowItems.get(position);
holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());
            holder.publicidadboton.setText(row_pos.getBoton());*/

            holder.publicidadboton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(position==0){
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        Bitmap myBitmap = QRCode.from(user.getUid()+"tienda1").bitmap();

                        holder.publicidadimagen.setImageBitmap(myBitmap);
                    }else {
                        TastyToast.makeText(v.getContext(),"segundo boton",TastyToast.LENGTH_SHORT,TastyToast.SUCCESS).show();
                    }
                }
            });


            convertView.setTag(holder);
        } else {
            holder = (CustomAdapterPublicidad.ViewHolder) convertView.getTag();
        }


            // Aquí cambia las propiedades de las vistas en el Adapter:
            RowItemPublicidad row_pos = rowItems.get(position);
             holder.publicidadimagen.setImageResource(row_pos.getFotoPublicidad());
            holder.publicidadboton.setText(row_pos.getBoton());


        return convertView;
    }

}
    
answered by 17.04.2017 в 23:07