public class CustomerProductAdapter extends BaseAdapter {
Context ccon;
ImageView cprdctimg;
LayoutInflater cprdctinflater;
View cprdctview;
TextView cprdctname,cprdctdesc,cprdctprice,cprdctquant,cpquantity;
ArrayList<Product> cproduct;
Product currentproduct;
Button cpincq,cpdecq;
@Override
public int getCount() {
return cproduct.size();
}
@Override
public Object getItem(int i) {
return cproduct.get(i).getPname();
}
public CustomerProductAdapter(Context con,ArrayList<Product> product) {
this.cproduct=product;
this.ccon = con;
cprdctinflater=LayoutInflater.from(con);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, final ViewGroup viewGroup) {
cprdctview=cprdctinflater.inflate(R.layout.cplist,null);
cpincq=cprdctview.findViewById(R.id.cpincq);
cpquantity=cprdctview.findViewById(R.id.cpquantity);
cpdecq=cprdctview.findViewById(R.id.cpdecq);
cprdctname=cprdctview.findViewById(R.id.cpname);
cprdctdesc=cprdctview.findViewById(R.id.cpdesc);
cprdctimg=cprdctview.findViewById(R.id.cppic);
cprdctprice=cprdctview.findViewById(R.id.cpprice);
cprdctquant=cprdctview.findViewById(R.id.cpquant);
currentproduct=cproduct.get(i);
cprdctimg.setImageResource(cproduct.get(i).getPimage());
cprdctname.append(cproduct.get(i).getPname().toString());
cprdctdesc.append(cproduct.get(i).getPdesc().toString());
cprdctprice.append(cproduct.get(i).getPprice().toString());
cprdctquant.append(cproduct.get(i).getPquant());
cpincq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentproduct.inccustomerquantity();
if(currentproduct.getcustomerquantity()>Integer.parseInt(currentproduct.getPquant()))
{
Toast.makeText(viewGroup.getContext(), "You cannot order more than stock", Toast.LENGTH_SHORT).show();
currentproduct.deccustomerquantity();
}
else {
cpquantity.setText("" + currentproduct.getcustomerquantity());
}
}
});
cpdecq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(currentproduct.deccustomerquantity()) {
cpquantity.setText("" + currentproduct.getcustomerquantity());
}
}
});
return cprdctview;
}
}
在这里,如果我增加列表中一种产品的数量,则相应的TextView不会更改,而是更改其他列表项中的其他TextView。从图像中可以看到,当我单击香蕉项目中的“ +”符号时,葡萄的数量就会改变。请帮助我解决此错误,以便仅对相应的textview进行更改。
答案 0 :(得分:0)
单击按钮时,需要更改列表中的模型,而不是UI。之后,更新适配器的所需位置。我建议您看一下ViewHolder模板。
答案 1 :(得分:0)
每次创建一行时都会调用GetView()。因此,您的全局/实例变量(当前乘积,cprdctname,cprdctdesc ... )始终引用最后创建的行(向上滚动时,底部滚动;向下滚动时,顶部滚动)。您可以将项目行的位置另存为按钮的 Tag ,并更新数据列表中的相应项目。然后调用适配器通过notifyDataSetChanged()重绘所有行。
尝试一下:
@Override
public View getView(int i, View view, final ViewGroup viewGroup) {
cprdctview=cprdctinflater.inflate(R.layout.cplist,null);
cpincq=cprdctview.findViewById(R.id.cpincq);
cpquantity=cprdctview.findViewById(R.id.cpquantity);
cpdecq=cprdctview.findViewById(R.id.cpdecq);
cprdctname=cprdctview.findViewById(R.id.cpname);
cprdctdesc=cprdctview.findViewById(R.id.cpdesc);
cprdctimg=cprdctview.findViewById(R.id.cppic);
cprdctprice=cprdctview.findViewById(R.id.cpprice);
cprdctquant=cprdctview.findViewById(R.id.cpquant);
currentproduct=cproduct.get(i);
cprdctimg.setImageResource(cproduct.get(i).getPimage());
cprdctname.append(cproduct.get(i).getPname().toString());
cprdctdesc.append(cproduct.get(i).getPdesc().toString());
cprdctprice.append(cproduct.get(i).getPprice().toString());
cprdctquant.append(cproduct.get(i).getPquant());
cpincq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentproduct = cproduct.get((int)view.getTag()); // Get item for the clicked position
currentproduct.inccustomerquantity();
if(currentproduct.getcustomerquantity()>Integer.parseInt(currentproduct.getPquant()))
{
Toast.makeText(viewGroup.getContext(), "You cannot order more than stock", Toast.LENGTH_SHORT).show();
currentproduct.deccustomerquantity();
}
else {
//cpquantity.setText("" + currentproduct.getcustomerquantity());
notifyDataSetChanged();
}
}
});
cpdecq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentproduct = cproduct.get((int)view.getTag()); // Get item for the clicked position
if(currentproduct.deccustomerquantity()) {
//cpquantity.setText("" + currentproduct.getcustomerquantity());
notifyDataSetChanged();
}
}
});
// Save the current position as Tag to the buttons
cpincq.setTag(i);
cpdecq.setTag(i);
return cprdctview;
}
希望有帮助!
答案 2 :(得分:0)
根据android官方文档,请勿重复使用位置。建议使用getAdapterPosition()。因此,在您的代码中,请使用listOfItems(getAdapterPosition())而不是listOfItems.get(i)