Aman Sharma Aman Sharma - 3 months ago 9
Java Question

when i scroll the lisview the textview get its default value 0 .how can i resolve this?

i have two button increase and decrease and one textview. when i click on increase button the value in the textview is increases and vice versa but when i scroll the listview its get its default value 0. how can i resolve this ??

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;
}

Answer

Save the value to PojoCart class and retrieve value from it every time just as you are getting itemName and itemPrice. So create an integer quantity in PojoCart class and in the adapter's getView method :

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();
};