I am using listview to populate data.Everything is fine until scroll it. While scrolling all the textview (COUNT) is changed to default value. Here I am using Cursor adapter to populate it.
When I clicked ^ / Down arrow in a item the next items value is changed.
NEXT ERROR:
When I click REMOVE Button, instead of remove the item which I clicked,other item is removed from the list.
Here is my AdapterClass:
public class CartCursorAdapter extends CursorAdapter implements View.OnClickListener {
public static final String MyPREFERENCES = "Preference";
public ViewHolder viewHolder;
DbUtil dbUtil;
LayoutInflater mInflater;
Context contextNew;
String MY_FRAGEMNT;
SharedPreferences pref;
String strItemName, strCategoryName, strPrice, strCartCount;
int price = 0;
String quantity;
int total;
String netA = "0";
int sum = 0;
float netAmount;
int cartPrice, cartSum, cartCount;
public CartCursorAdapter(Context context, Cursor c, String MYFRAGMENT, boolean autoRequery) {
super(context, c, autoRequery);
mInflater = LayoutInflater.from(context);
contextNew = context;
MY_FRAGEMNT = MYFRAGMENT;
dbUtil = new DbUtil(context);
dbUtil.open();
pref = context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
strItemName = pref.getString("itemName", null);
strCategoryName = pref.getString("categoryName", null);
strPrice = pref.getString("Price", null);
strCartCount = pref.getString("cartCount", null);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater.inflate(R.layout.cart_list_item, null);
return v;
}
@Override
public void bindView(View itemView, Context context, Cursor cursor) {
viewHolder = (ViewHolder) itemView.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder();
viewHolder.cartProduct = (TextView) itemView.findViewById(R.id.cartProduct);
viewHolder.cartQuantity = (TextView) itemView.findViewById(R.id.cartQuantity);
viewHolder.cartPrice = (TextView) itemView.findViewById(R.id.cartPrice);
viewHolder.cartPriceDum = (TextView) itemView.findViewById(R.id.cartPriceDum);
viewHolder.cartCount = (TextView) itemView.findViewById(R.id.cartCount);
viewHolder.ivDecrease = (ImageView) itemView.findViewById(R.id.ivDecrease);
viewHolder.ivIncrease = (ImageView) itemView.findViewById(R.id.ivIncrease);
viewHolder.addTowish = (Button) itemView.findViewById(R.id.addTowish);
viewHolder.remove = (Button) itemView.findViewById(R.id.remove);
viewHolder.cardView = (CardView) itemView.findViewById(R.id.cardlist_item);
viewHolder.ivDecrease.setOnClickListener(this);
viewHolder.ivIncrease.setOnClickListener(this);
viewHolder.addTowish.setOnClickListener(this);
viewHolder.remove.setOnClickListener(this);
itemView.setTag(viewHolder);
}
if (MY_FRAGEMNT == "CheckOutFragment") {
viewHolder.addTowish = (Button) itemView.findViewById(R.id.addTowish);
viewHolder.remove = (Button) itemView.findViewById(R.id.remove);
viewHolder.addTowish.setVisibility(View.GONE);
viewHolder.remove.setVisibility(View.GONE);
viewHolder.cardView.setCardElevation(0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
viewHolder.cardView.setLayoutParams(layoutParams);
itemView.setTag(viewHolder);
}
String pdtName = cursor.getString(cursor.getColumnIndex(DbHelper.CART_PDT_NAME));
String catName = cursor.getString(cursor.getColumnIndex(DbHelper.CART_CAT_NAME));
int cartprice = cursor.getInt(cursor.getColumnIndex(DbHelper.CART_PRICE));
int strquantity = cursor.getInt(cursor.getColumnIndex(DbHelper.CART_QUANTITY));
viewHolder.cartProduct.setText(pdtName);
viewHolder.cartPrice.setText(String.valueOf(cartprice));
viewHolder.cartQuantity.setText(catName);
viewHolder.cartCount.setText(String.valueOf(strquantity));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.remove:
dbUtil.open();
String delItem = viewHolder.cartProduct.getText().toString();
Cursor Cartcursor = dbUtil.getCartID(delItem);
if (Cartcursor != null && Cartcursor.moveToFirst()) {
Cartcursor.moveToFirst();
String strCartProductID = Cartcursor.getString(Cartcursor.getColumnIndex(DbHelper.CART_PDT_ID));
dbUtil.deleteCart(strCartProductID, delItem);
Toast.makeText(contextNew, "Cart Item " + "RowId" + strCartProductID + " Product Id" + delItem, Toast.LENGTH_SHORT).show();
Toast.makeText(contextNew, "Deleted Successfully", Toast.LENGTH_SHORT).show();
}
break;
case R.id.addTowish:
break;
case R.id.ivIncrease:
increase();
break;
case R.id.ivDecrease:
decrease();
break;
}
}
private void decrease() {
strPrice = viewHolder.cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(viewHolder.cartProduct.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter--;
if (counter > 0) {
viewHolder.cartProduct.setText(Integer.toString(counter));
viewHolder.cartPrice.setVisibility(View.GONE);
viewHolder.cartPriceDum.setVisibility(View.VISIBLE);
quantity = viewHolder.cartProduct.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum -= price;
netAmount = sum;
viewHolder.cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
// Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
// Toast.makeText(context, "Minimum Item is 1", Toast.LENGTH_SHORT).show();
}
}
}
private void increase() {
strPrice = viewHolder.cartPrice.getText().toString();
price = Integer.parseInt(strPrice);
int counter = 0;
try {
counter = Integer.parseInt(viewHolder.cartCount.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
}
counter++;
if (counter > 0) {
viewHolder.cartCount.setText(Integer.toString(counter));
viewHolder.cartPrice.setVisibility(View.GONE);
viewHolder.cartPriceDum.setVisibility(View.VISIBLE);
quantity = viewHolder.cartCount.getText().toString();
total = (Integer.parseInt(quantity)) * (price);
netA = String.valueOf(total);
sum += price;
netAmount = sum;
viewHolder.cartPriceDum.setText(String.valueOf(total));
cartCount = Integer.parseInt(quantity);
// Toast.makeText(context, "netAmount" + netAmount + "\n" + "Total" + total, Toast.LENGTH_SHORT).show();
if (counter == 1) {
cartPrice = price;
cartSum = sum;
}
if (counter == 0) {
cartPrice = 0;
cartSum = 0;
cartCount = 0;
}
}
}
public static class ViewHolder {
public static Button addTowish, remove;
public TextView cartProduct, cartQuantity, cartPrice, cartCount, cartPriceDum;
public ImageView ivDecrease, ivIncrease;
CardView cardView;
}
}