Because textview product counter is repeated in grid view using BaseAdapter

63 Views Asked by At

Because it repeats the product counter which is a TextView; I have the following scenario:

  1. The user selects the product, and applies the quantity.
  2. From the Activity and not from the Adapter I show the quantity of the selected product as seen in the image.
  3. Everything works perfect, until under the screen and the counter magically passes to another product, I have implemented these methods by suggestion:

    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @Override
    public int getItemViewType(int position)
    {
        return position;
    }
    

and still I still get this error. I have also tried to modify the counter from the Adapter but the same thing happens. I want that counter to stay fixed and not update when under the screen.

Adapter:

   public class ProductoAdapter extends BaseAdapter
   {
    private Activity activity;
    private LayoutInflater inflater;
    private ArrayList<Producto> listadoProductos;
    private ArrayList<Producto> productosPedidos;

    Producto producto;

    Empresas pedido;

    public final int TYPE_NOTICIA=0;
    public final int TYPE_LOAD=1;
    private gestionSharedPreferences sharedPreferences;
    private Context context;
/*
    OnLoadMoreListener loadMoreListener;
*/
    boolean isLoading=false, isMoreDataAvailable=true;
    vars vars;
    public static int contador;

    public static int i;
    public static int contadorGeneral;
    private NumberFormat numberFormat;
    ImageLoader imageLoader = ControllerSingleton.getInstance().getImageLoader();

    public ProductoAdapter(Activity activity, ArrayList<Producto> listadoProductos)
    {
        this.activity=activity;
        this.listadoProductos=listadoProductos;
        productosPedidos=new ArrayList<Producto>();
        vars=new vars();
        sharedPreferences=new gestionSharedPreferences(this.activity);
        contador=0;
        contadorGeneral=0;
        producto=new Producto();

       /* LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver,
                new IntentFilter("custom-message"));*/
    }

  /*  public BroadcastReceiver mMessageReceiver = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // Get extra data included in the Intent
            producto = (Producto) intent.getSerializableExtra("producto");
            Toast.makeText(context, producto.getNombreProducto()+producto.getNumeroDeProducto() ,Toast.LENGTH_SHORT).show();
        }
    };*/


    @Override
    public int getCount()
    {
        return listadoProductos.size();
    }

    @Override
    public Producto getItem(int position)
    {
        return listadoProductos.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public int getItemViewType(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup)
    {
        numberFormat = NumberFormat.getNumberInstance(Locale.GERMAN);

        if (view == null)
        {
            LayoutInflater inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);

            Log.i("martin","null");
        }
        else
        {
            Log.i("martin","llenoooooooooo");
        }
        final Producto producto = getItem(position);

        ImageView imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);

        TextView nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
        TextView cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
        TextView precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
        TextView precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
        TextView ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);



        if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
        {
            imagenProducto.setImageResource(R.drawable.ic_not_image_found);
        }

        else
        {
            Glide.with(activity).
                    load(producto.getImagenProducto().toString()).
                    thumbnail(0.5f).into(imagenProducto);
        }

        nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());

        //cantidadProducto.setText(producto.getCantidadProducto());
        precioGeneralProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioGeneralProducto())));
        precioGeneralProducto.setPaintFlags(precioGeneralProducto.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
        precioPideProducto.setText("$"+numberFormat.format(Double.parseDouble(producto.getPrecioPideProducto())));
        int valorahorro=(Integer.parseInt(producto.getPrecioGeneralProducto()))-(Integer.parseInt(producto.getPrecioPideProducto()));
        ahorroProducto.setText(""+"$"+numberFormat.format(Double.parseDouble(""+valorahorro)));

        return view;
    }

}

Activity:

I use a TextView as a counter which is initially invisible, but when it is selected, it is visible with the quantity of product selected.

From the activity I use this method:

 @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        Producto producto = (Producto) parent.getItemAtPosition(position);

        final View contadorImagenProductoBolsa = Pedidos.this.findViewById(R.id.contadorProductoImagenBolsa);
        TextView test1TextView = (TextView) view.findViewById(R.id.contadorProductoImagenBolsa);


        mostrarDialogoDetalle(test1TextView,position,producto.getIdProducto(),producto.getImagenProducto(), producto.getNombreProducto(),
                producto.getCantidadProducto(),producto.getUnidadProducto(), producto.getPrecioGeneralProducto(),producto.getPrecioPideProducto(),
                producto.getAhorroProducto());

    }

Has anyone had this happen? how did they solve it?

enter image description here

Here it is perfect, but when I go under the screen and I go back and up, observe how the counter happens to another product inexplicably:

enter image description here

I want an effective help and a possible snippet if it is the case of an incorrect adapter or activity.

Thank you.

1

There are 1 best solutions below

0
On

I think You have to add ViewHolder For ProductoAdapter Like:

class ViewHolder
{
      TextView nombreProducto,cantidadProducto
      ,precioGeneralProducto,precioPideProducto,ahorroProducto;
      ImageView imagenProducto;

public ViewHolder(View view){

     imagenProducto = (ImageView) view.findViewById(R.id.imagenProducto);
     nombreProducto = (TextView) view.findViewById(R.id.nombreProducto);
     cantidadProducto = (TextView) view.findViewById(R.id.cantidadProducto);
     precioGeneralProducto = (TextView) view.findViewById(R.id.precioGeneralProducto);
     precioPideProducto = (TextView) view.findViewById(R.id.precioPideProducto);
     ahorroProducto = (TextView) view.findViewById(R.id.ahorroProducto);


}
}

Use setTag and getTag For ViewHolder Like:

ViewHolder holder;
    if (view == null)
    {
        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.producto_row_layout, viewGroup, false);
        holder = new ViewHolder();
        Log.i("martin","null");
    }
    else
    {    holder = (ViewHolder) view.getTag();
        Log.i("martin","llenoooooooooo");
    }

    final Producto producto = getItem(position);
    if (producto.getImagenProducto().toString().equals("http://fasttrackcenter.com/pide/app/"))
    {
        holder.imagenProducto.setImageResource(R.drawable.ic_not_image_found);
    }

    else
    {
        Glide.with(activity).
                load(producto.getImagenProducto().toString()).
                thumbnail(0.5f).into(holder.imagenProducto);
    }

    holder.nombreProducto.setText(producto.getNombreProducto()+" x "+producto.getCantidadProducto()+" "+producto.getUnidadProducto());
    .
    .
    .
 view.setTag(holder);
    return view;

i hope this is help you