因为textview产品计数器在网格视图中使用BaseAdapter重复
问题描述:
因为它重复了一个TextView产品计数器;我有以下情况:因为textview产品计数器在网格视图中使用BaseAdapter重复
- 用户选择产品并应用数量。
- 从活动而不是从适配器我显示所选产品的数量,如图中所示。
-
一切都运行完美,直到屏幕下方和柜台神奇地传递到另一种产品,我已经建议实施这些方法:
@Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; }
,仍然我仍然得到这个错误。我也尝试从适配器修改计数器,但同样的事情发生。我希望该计数器保持不变,并且不会在屏幕下更新。
适配器:
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());
}
有没有人有这种情况发生?他们是如何解决它的?
这是完美的,但是当我走在屏幕下,我回去了,观察计数器是如何发生的另一个产品莫名:
我想要的有效的帮助和一个可能的片段,如果是不正确的适配器或活动的情况。
谢谢。
答
我认为你必须添加ViewHolder对于ProductoAdapter像:
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);
}
}
使用setTag和getTag对于ViewHolder像:
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;
我希望这是帮助你
+0
不工作,它不断重复..帮助! – FABiO
您可以选择存储点击.so时的特定位置,而您滚动或移动该位置时只显示文本。这是因为你的listview被重新使用单元格。 –