因为使用BaseAdapter在网格视图中重复textview产品计数器

时间:2017-09-13 06:23:34

标签: android baseadapter android-gridview onitemclick getview

因为它重复了TextView的产品计数器;我有以下情况:

  1. 用户选择产品,然后应用数量。
  2. 从活动而不是从适配器我显示所选产品的数量,如图所示。
  3. 一切都很完美,直到屏幕下方和计数器神奇地传递给另一个产品,我已经通过建议实施了这些方法:

    @Override
    public long getItemId(int position)
    {
        return position;
    }
    
    @Override
    public int getItemViewType(int position)
    {
        return position;
    }
    
  4. 仍然我仍然遇到此错误。我也尝试从适配器修改计数器,但同样的事情发生。我希望该计数器保持固定状态,而不是在屏幕下更新。

    适配器:

       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;
        }
    
    }
    

    的活动:

    我使用TextView作为最初不可见的计数器,但是当它被选中时,它会随着所选产品的数量而可见。

    从活动中我使用这种方法:

     @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());
    
        }
    

    有人有过这种情况吗?他们是如何解决的?

    enter image description here

    这里很完美,但是当我走到屏幕下面并且我回头看时,请注意另一种产品莫名其妙地发生在另一种产品上:

    enter image description here

    如果是不正确的适配器或活动,我需要有效的帮助和可能的代码段。

    谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您必须为ProductoAdapter添加ViewHolder,如:

find -type f -iname "file*" -exec awk 'function check(array,val,count){ ##Using find command to get only the files in a directory, using exec passing their values to awk too.From here awk code starts, creating a function named check here, which will have parameters array,val and count to be passed into it, whenever a call is being made to it.
        if(length(array)==count){                    ##Checking here if length of array is equal to variable count, if yes then do following action.
           system("echo cp " val " destination_path")##Using awks system function here by which we could execute shell commands in awk script, so I have written here echo to only check purposes initially, it will print copy command if any files al lines are matching to Input_file file, if OP is happy with it OP should remove echo then.
}
}
FNR==NR{                                             ##FNR==NR condition will be only TRUE when very first file named Input_file is being read.
  a[$0];                                             ##creating an array named a whose index is current line.
  next                                               ##using next keyword will skip all further statements.
}
val!=FILENAME{                                       ##checking here when variable val is not having same value as current file name then perform following actions.
  check(a,val,count)                                 ##calling check function with passing arguments of array a,val,count.
}
FNR==1{                                              ##Checking if FNR==1, which will be true whenever a new files first line is being read.
  val=FILENAME;                                      ##creating variable named val whose value is current Input_file filename.
  count=total="";                                    ##Nullifying variables count and total now.
  delete b                                           ##Deleting array b here.
}
($1 in a) && !b[$1]++{                               ##Checking if first field of file is in array a and it is not present more than 1 time in array b then do following
  count++                                            ##incrementing variable named count value to 1 each time cursor comes inside here.
}
END{                                                 ##starting awk END block here.
  check(a,val,count)                                 ##Calling function named check with arguments array a,val and count in it.
}
' Input_file {} +                                    ##Mentioning Input_file here

使用setTag和getTag For ViewHolder赞:

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);


}
}

我希望这对你有帮助