滚动列表视图时TextView的值已更改

时间:2016-03-01 12:20:40

标签: android listview scroll android-cursoradapter

我正在使用listview来填充数据。在滚动之前一切正常。滚动所有文本视图(COUNT)时更改为默认值。这里我使用Cursor适配器来填充它。

enter image description here

当我点击某个项目中的 ^ / 向下箭头时,会更改下一个项目值。

NEXT ERROR:

点击删除按钮,而不是删除我点击的项目,其他项目将从列表中删除。

这是我的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;
    }
}

1 个答案:

答案 0 :(得分:0)

对于REMOVE: 创建自定义OnClickListener,将cartProduct作为其参数

 public class CustomOnClick implements OnClickListener {
    String product;


    public CustomOnClick(String product) {
        super();
        this.product = product;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        dbUtil.open();
            String delItem = product; //set product to delItem
            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();
            }
    }
}

然后在设置onclicklistener删除按钮时,您可以尝试

  viewHolder.remove.setOnClickListener(new CustomOnClick(cursor.getString(cursor.getColumnIndex(DbHelper.CART_PDT_NAME))));

当您在HashMap中加载listview添加值时存储COUNT值(存储产品名称和计数),每当您增加或减少COUNT时,只需在hashmap中更新其值