hasiertxo
hasiertxo

Reputation: 263

Repeated ImageView in ListView every 4 item

Here's my problem:

I have a ListView with an ImageView that at first is set to GONE, but after doing some stuff on the custom adapter y set it to visible.

My problem is that if I select the first element of the list,every four item also sets it's image to VISIBLE.

I hope I've explained myself. If any doubt don't hesitate to ask me.

I edit to add some code. Some parts are in Spanish as I am Spanish.

getView(): View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_view_item, null);
        }
        final Oferta o = items.get(position);
        boolean comprado = false;
        if (o != null) {
                final ImageView image = (ImageView) v.findViewById(R.id.imageView1);
                TextView titulo = (TextView) v.findViewById(R.id.textView1);
                TextView precioAnterior = (TextView) v.findViewById(R.id.textView2);
                TextView precioNuevo = (TextView) v.findViewById(R.id.textView3);
                TextView fechaHasta = (TextView) v.findViewById(R.id.textView4);
                ImageView imageComprado = (ImageView) v.findViewById(R.id.imageView2);

                Drawable compra = this.getContext().getResources().getDrawable(R.drawable.comprada);


                for(int i = 0; i < MainActivity.tickets.size();i++)
                {


                    if((MainActivity.tickets.get(i).getOferta().getId()) == (o.getId()))
                    {   
                        System.out.println("tickets: " + MainActivity.tickets.get(i).getOferta().getId());
                        System.out.println("oferta: " + o.getId());
                        comprado  = true;
                        break;
                    }
                }

                if (comprado)
                {
                    imageComprado.setVisibility(View.VISIBLE);
                    comprado = false;
                }

                if (titulo != null) {
                      titulo.setText(o.getTitulo());    
                      }
                if(precioAnterior != null){
                    precioNuevo.setText(String.valueOf(new java.text.DecimalFormat("#.##").format(o.getPrecioNuevo()))+"€");    
                }
                if(precioNuevo != null){
                    precioAnterior.setPaintFlags(precioAnterior.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    precioAnterior.setText(String.valueOf(new java.text.DecimalFormat("#.##").format(o.getPrecioAnterior()))+"€");  
                }
                if(fechaHasta != null){
                    fechaHasta.setText("Cad.: " + o.getDisponibleHasta());  
                }
                if(image != null){
                     DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                    .cacheInMemory()
                    .cacheOnDisc()
                    .build();
                 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
                    .defaultDisplayImageOptions(defaultOptions)
                    .build();



                 ImageLoader.getInstance().init(config);
                //imageLoader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
                ImageLoader.getInstance().displayImage(o.getImagen(), image);
                      }   

Upvotes: 0

Views: 566

Answers (2)

rahul
rahul

Reputation: 6487

It is due to recycling of list items. Just track your items whose imageView you want to set to Visible in a List. And in your CustomAdapter, try check if the current index is in the list. If not set it to GONE else visible.

Upvotes: 0

Zala Janaksinh
Zala Janaksinh

Reputation: 2927

My Answer Is below Try this code and done your job.

public View getView(int position, View convertView, ViewGroup parent) 
    {   
        try
        {
            View row = null;

            if (row == null) 
            {   
                LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.custom_list_item, parent, false);

                // Get reference  
                imgIconView = (ImageView) row.findViewById(R.id.imgIcon);
                txtLabel = (TextView) row.findViewById(R.id.txtLable);
                txtLabel.setTypeface(genHelper.setFontFace("TektonPro-Bold.otf"),Typeface.BOLD);

                txtScore=(TextView) row.findViewById(R.id.txtScore);
                txtScore.setTypeface(genHelper.setFontFace("TektonPro-Bold.otf"),Typeface.BOLD);

            }else
            {
                row = (View) convertView;
            }
}

copy this code and replace your own getview method code it`s work dear.

Upvotes: 0

Related Questions