滚动时ListView项中的重复值

时间:2016-11-22 10:55:04

标签: android listview baseadapter

我在listview中有一个增加按钮的OnClickListener()项目增加数量中的这个按钮在testview中显示问题是当我滚动列表视图中的另一个项目变为增加的相同数字时。

这是我的Adpater

public class ProductsAdapter extends BaseAdapter {

List<ProductSubModel> myList;
LayoutInflater inflater;
Context context;
int i;

public ProductsAdapter(Context context, List<ProductSubModel> myList) {
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);
}

@Override
public int getCount() {
    return myList.size();
}

@Override
public Object getItem(int position) {
    return myList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup viewGroup) {

    final MyViewHolder mViewHolder;
    i = 0;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.adapter_product, viewGroup, false);
        mViewHolder = new MyViewHolder(convertView);
        convertView.setTag(mViewHolder);
    } else {
        mViewHolder = (MyViewHolder) convertView.getTag();
    }

    mViewHolder.productImageView.setScaleType(ImageView.ScaleType.FIT_XY);
    Glide.with(context)
            .load(myList.get(position).getImage())
            .placeholder(R.drawable.nadarah_icon)
            .override(250, 250)
            .into(mViewHolder.productImageView);

    mViewHolder.productNameTextView.setText(myList.get(position).getProductName());
    mViewHolder.productDescribtionTextView.setText("DESC : " + myList.get(position).getShortDescription());
    mViewHolder.productPriceTextView.setText("PRISE : " + myList.get(position).getPrice());
    mViewHolder.discountPriceTextView.setText("DISCOUNT : " + myList.get(position).getMinimalPrice());
    mViewHolder.qtyFromApiTextView.setText("QTY : " + 20);
    mViewHolder.minusImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (i > 0) {
                i--;
                mViewHolder.qtyUserBuyTextView.setText("" + i);
            } else {
                Utils.msg(context , "");
            }
        }
    });
    mViewHolder.plusImageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (i < 20) {
                i++;
                mViewHolder.qtyUserBuyTextView.setText("" + i);
            } else {
                Utils.msg(context , "");
            }
        }
    });
    mViewHolder.addtoCartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    return convertView;
}


private class MyViewHolder {
    ImageView productImageView;
    TextView productNameTextView;
    TextView productDescribtionTextView;
    TextView productPriceTextView;
    TextView discountPriceTextView;
    TextView qtyFromApiTextView;
    ImageView minusImageView;
    ImageView plusImageView;
    TextView qtyUserBuyTextView;
    TextView addtoCartButton;
    TextView productIdTextView;


    public MyViewHolder(View item) {
        productImageView = (ImageView) item.findViewById(R.id.productImageView);
        productNameTextView = (TextView) item.findViewById(R.id.productNameTextView);
        productDescribtionTextView = (TextView) item.findViewById(R.id.productDescribtionTextView);
        productPriceTextView = (TextView) item.findViewById(R.id.productPriceTextView);
        discountPriceTextView = (TextView) item.findViewById(R.id.discountPriceTextView);
        qtyFromApiTextView = (TextView) item.findViewById(R.id.qtyFromApiTextView);
        minusImageView = (ImageView) item.findViewById(R.id.minusImageView);
        qtyUserBuyTextView = (TextView) item.findViewById(R.id.qtyUserBuyTextView);
        plusImageView = (ImageView) item.findViewById(R.id.plusImageView);
        addtoCartButton = (TextView) item.findViewById(R.id.addtoCartButton);
        productIdTextView = (TextView) item.findViewById(R.id.productIdTextView);
    }

}
}

这是适配器实现

private void implementAdpater(List<ProductSubModel> productSubModels) {
    this.productSubModels = productSubModels;
    productsAdapter = new ProductsAdapter(getActivity(), productSubModels);
    productsInCategoryList.setAdapter(productsAdapter);
    productsAdapter.notifyDataSetChanged();
}

2 个答案:

答案 0 :(得分:1)

删除productsAdapter.notifyDataSetChanged();在setadapter()之后并检查一次。

查看此供您参考: http://www.journaldev.com/10416/android-listview-with-custom-adapter-example-tutorial

答案 1 :(得分:1)

ProductSubModel中应该有一个数量的字段(或任何增加或减少的字段),并且每次增加模型中的增量。

i++;
mViewHolder.qtyUserBuyTextView.setText("" + i);
myList.get(position).setQunatity(myList.get(position).getQunatity()+1);

听众之外

mViewHolder.qtyUserBuyTextView.setText("" + myList.get(position).getQunatity());

相同的减量