我总是收到同样的东西
I'm always receiving the same item
创建的每个项目都是一个具有名称、价格和数量的产品类型的对象。碰巧当我试图在 gridview 中接收每个项目的位置时,我总是得到视图的第一个项目。在 post 解决我的问题之前,我看了其他几个 post ,看起来我做的一切都是正确的,以便如何使用标签。
我有
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null || (convertView.getTag() == null)) {
convertView = inflater.inflate(R.layout.listview_values, parent, false);
holder = new ViewHolder();
holder.productName = (TextView) convertView.findViewById(R.id.textViewList1);
holder.productQuantity = (EditText) convertView.findViewById(R.id.EditTextList2);
holder.productPrice = (TextView) convertView.findViewById(R.id.textViewList3);
holder.removeProduct = (ImageButton) convertView.findViewById(R.id.removeProduct);
}
else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.productName.setText(products.get(position).getProductName());
holder.productQuantity.setText(String.valueOf(products.get(position).getProductQuantity()) + " Un.");
holder.productPrice.setText(String.valueOf(products.get(position).getProductPrice())+ " €");
holder.removeProduct.setTag(new Integer(position));
return convertView;
}
和
public void removeProduct(View v) {
ImageButton removeProduct = (ImageButton) findViewById(R.id.removeProduct);
Integer position = (Integer) removeProduct.getTag();
Product p1 = (Product) adapter.getItem(position);
Toast toast = Toast.makeText(this,"You want to remove the item: "+ position +" with the product name: "+ p1.getProductName() + " , quantity: "+ p1.getProductQuantity()
+ " , price: " + p1.getProductPrice(),Toast.LENGTH_LONG); toast.show();
}
当我 运行 模拟器现在不管我点击什么项目我总是得到这个结果:
如果您需要查看更多代码,请告诉我,我会post。提前谢谢你们。
将您的 removeProduct()
更改为 -
public void removeProduct(View v) {
Integer position = (Integer) v.getTag();
Product p1 = (Product) adapter.getItem(position);
Toast toast = Toast.makeText(this,"You want to remove the item: "+ position +" with the product name: "+ p1.getProductName() + " , quantity: "+ p1.getProductQuantity()
+ " , price: " + p1.getProductPrice(),Toast.LENGTH_LONG); toast.show();
}
原因是您再次通过
初始化您的视图
ImageButton removeProduct = (ImageButton) findViewById(R.id.removeProduct)
这不再是您点击的那个视图。
你点击的view是传入的参数removeProduct()
创建的每个项目都是一个具有名称、价格和数量的产品类型的对象。碰巧当我试图在 gridview 中接收每个项目的位置时,我总是得到视图的第一个项目。在 post 解决我的问题之前,我看了其他几个 post ,看起来我做的一切都是正确的,以便如何使用标签。
我有
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null || (convertView.getTag() == null)) {
convertView = inflater.inflate(R.layout.listview_values, parent, false);
holder = new ViewHolder();
holder.productName = (TextView) convertView.findViewById(R.id.textViewList1);
holder.productQuantity = (EditText) convertView.findViewById(R.id.EditTextList2);
holder.productPrice = (TextView) convertView.findViewById(R.id.textViewList3);
holder.removeProduct = (ImageButton) convertView.findViewById(R.id.removeProduct);
}
else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setTag(holder);
holder.productName.setText(products.get(position).getProductName());
holder.productQuantity.setText(String.valueOf(products.get(position).getProductQuantity()) + " Un.");
holder.productPrice.setText(String.valueOf(products.get(position).getProductPrice())+ " €");
holder.removeProduct.setTag(new Integer(position));
return convertView;
}
和
public void removeProduct(View v) {
ImageButton removeProduct = (ImageButton) findViewById(R.id.removeProduct);
Integer position = (Integer) removeProduct.getTag();
Product p1 = (Product) adapter.getItem(position);
Toast toast = Toast.makeText(this,"You want to remove the item: "+ position +" with the product name: "+ p1.getProductName() + " , quantity: "+ p1.getProductQuantity()
+ " , price: " + p1.getProductPrice(),Toast.LENGTH_LONG); toast.show();
}
当我 运行 模拟器现在不管我点击什么项目我总是得到这个结果:
如果您需要查看更多代码,请告诉我,我会post。提前谢谢你们。
将您的 removeProduct()
更改为 -
public void removeProduct(View v) {
Integer position = (Integer) v.getTag();
Product p1 = (Product) adapter.getItem(position);
Toast toast = Toast.makeText(this,"You want to remove the item: "+ position +" with the product name: "+ p1.getProductName() + " , quantity: "+ p1.getProductQuantity()
+ " , price: " + p1.getProductPrice(),Toast.LENGTH_LONG); toast.show();
}
原因是您再次通过
初始化您的视图 ImageButton removeProduct = (ImageButton) findViewById(R.id.removeProduct)
这不再是您点击的那个视图。
你点击的view是传入的参数removeProduct()