代码表示点击购物车图片时,产品名称会变为红色。这适用于listview
中显示的第一项(仅限您可以在手机屏幕上看到的项目)。
向下滚动以查看listview
中的其他项目是否有效但是当我点击项目将其添加到购物车时,应用程序会崩溃:
parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.VISIBLE);
使用NullPointerException
我没有看到可能导致问题的原因以及一些帮助它会受到影响。谢谢。
@Override
public int getCount() {
return listTransferObjectProduct.size();
}
@Override
public Object getItem(int position) {
return listTransferObjectProduct.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class Holder {
TextView textViewProduct;
TextView textViewNameChangeColor;
ImageView addOneItem;
TextView textViewPrice;
}
Holder holder;
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
product = listTransferObjectProduct.get(position);
holder = new Holder();
convertView = inflater.inflate(R.layout.custom_product_adapter, null);
holder.textViewProduct = (TextView) convertView.findViewById(R.id.textViewName);
holder.textViewPrice = (TextView) convertView.findViewById(R.id.textViewPrice);
holder.textViewNameChangeColor =(TextView) convertView.findViewById(R.id.textViewNameChangeColor);
holder.textViewNameChangeColor.setVisibility(View.INVISIBLE);
holder.addOneItem = (ImageView) convertView.findViewById(R.id.imageViewAdd);
holder.textViewProduct.setText(product.articleName);
holder.textViewNameChangeColor.setText(product.articleName);
holder.textViewPrice.setText(Double.toString(listTransferObjectProduct.get(position).price) + " $");
holder.addOneItem.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
utility.incrementCounter((Activity) context);
product = listTransferObjectProduct.get(position);
product.quantity = 1;
parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.VISIBLE);
utility.saveToSharedPrefereces(product, true);
return true;
case MotionEvent.ACTION_UP:
parent.getChildAt(position).findViewById(R.id.textViewNameChangeColor).setVisibility(View.INVISIBLE);
return true;
}
return false;
}
});
return convertView;
}