当我滚动listview时,textview的默认值为0

时间:2016-08-10 11:17:06

标签: java android

我有两个按钮增加和减少以及一个textview。当我单击“增加”按钮时,textview中的值会增加,反之亦然,但是当我滚动listview时,它会获得默认值0.我该如何解决这个问题?

public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.cart_item, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.mImgItem = (ImageView) convertView.findViewById(R.id.cart_image);
            viewHolder.mTvItemName = (TextView) convertView.findViewById(R.id.tv_item_name);
            viewHolder.mTvItemPrice = (TextView) convertView.findViewById(R.id.tv_item_price);
            viewHolder.mTvNumber = (TextView) convertView.findViewById(R.id.tv_number);
            viewHolder.mBtnAdd = (Button) convertView.findViewById(R.id.btn_add);
            viewHolder.mBtnMinus = (Button) convertView.findViewById(R.id.btn_sub);
            viewHolder.mImgDelete = (ImageView) convertView.findViewById(R.id.img_del);
            viewHolder.mUniqueKey = String.valueOf(position);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.mBtnAdd.setTag(viewHolder);
        viewHolder.mBtnMinus.setTag(viewHolder);

        viewHolder.mTvNumber.setText("0");
        viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder tagHolder = (ViewHolder) v.getTag();

                int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString().trim());
                mValue++;
                tagHolder.mTvNumber.setText("" + mValue);    
        });

        viewHolder.mBtnMinus.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewHolder tagHolder = (ViewHolder) v.getTag();
                    int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString());
                    if (mValue > 0) {
                        mValue--;
                        tagHolder.mTvNumber.setText("" + mValue);
                    }

        });

        convertView.setTag(viewHolder);

        PojoCart pojoCart = (PojoCart) getItem(position);
        viewHolder.mTvItemName.setText(pojoCart.getmItemName());
        viewHolder.mTvItemPrice.setText(pojoCart.getmItemPrice());

        return convertView;
    }

4 个答案:

答案 0 :(得分:1)

将值保存到PojoCart类,并在每次获取itemName和itemPrice时从中检索值。因此,在PojoCart类和适配器的getView方法中创建一个整数:

PojoCart pojoCart = (PojoCart) getItem(position);
viewHolder.mBtnAdd.setTag(pojoCart);
viewHolder.mBtnMinus.setTag(pojoCart);
if(pojoCart.getQuantity() > 0)
{
   viewHolder.mTvNumber.setText(""+pojoCart.getQuantity());
}
else
{
   viewHolder.mTvNumber.setText(""+0);
}
viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

          PojoCart pojoCart = (PojoCart) v.getTag();
            int mValue = pojoCart.getQuantity();
            mValue++;
            tagHolder.mTvNumber.setText("" + mValue);
            pojoCart.setQuantity(mValue)
            notifyDataSetChanged();
};

答案 1 :(得分:0)

您必须保存textview的价值。 当listview重新创建滚动视图时,它采用默认方式 只需获取模型类并按位置保存值

答案 2 :(得分:0)

使用adapter.notifyDataSetChanged()在数据更改时更新listview中的值

答案 3 :(得分:0)

您需要在每个列表项中存储该数字,例如使用此函数来更改列表项的数字的值:

List<Integer> mList = new ArrayList<>(LIST_SIZE);

int changeNumber(int position, int value){
    mList.get(position) += value;
    return mList.get(position);
}
你可以使用这个函数:

viewHolder.mBtnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewHolder tagHolder = (ViewHolder) v.getTag();

            int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString().trim());
            tagHolder.mTvNumber.setText("" + changeNumber(position,1);    
    });

    viewHolder.mBtnMinus.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewHolder tagHolder = (ViewHolder) v.getTag();
                int mValue = Integer.parseInt(tagHolder.mTvNumber.getText().toString());
                if (mValue > 0) {
                    tagHolder.mTvNumber.setText("" + changeNumber(position,-1);
                }

    });