Android 文本视图';滚动listview时更改了s值

Android 文本视图';滚动listview时更改了s值,android,listview,scroll,android-cursoradapter,Android,Listview,Scroll,Android Cursoradapter,我正在使用listview填充数据。在滚动之前,一切都正常。滚动时,所有文本视图(计数)都更改为默认值。这里我使用游标适配器来填充它 当我在项目中单击^/向下箭头时,下一个项目的值将更改 下一个错误: 当我单击“删除”按钮时,将从列表中删除其他项目,而不是删除我单击的项目 这是我的AdapterClass: public class CartCursorAdapter extends CursorAdapter implements View.OnClickListener { pu

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

当我在项目中单击^/向下箭头时,下一个项目的值将更改

下一个错误:

当我单击“删除”按钮时,将从列表中删除其他项目,而不是删除我单击的项目

这是我的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;
    }
}
删除: 创建以cartProduct为参数的自定义OnClickListener

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

为了在加载listview时存储计数值,在HashMap中添加值(存储产品名称和计数),在每次增加或减少计数后,只需在HashMap中更新其值

我已将所有值存储到帮助我修复错误的
列表中。