【问题标题】:Anonymous bug in TextViewTextView 中的匿名错误
【发布时间】:2016-05-25 06:11:13
【问题描述】:

我正在开发一个餐厅应用程序。在这里,我使用购物车页面来查看用户选择结帐的食品。购物车项目使用 RecyclerView 列出,该视图具有 + 和 - 按钮来增加和减少结帐前的产品数量。我的问题就在这里。

让我们考虑如下项目:

                                   Result TextView
ChocoBar    Rs25     -   4   +   =      100      // Expected Result

Vennila     Rs30     -   1   +   =      30

点击ChocoBar中的“+”按钮时

                                  Result TextView
ChocoBar    Rs25     -   5   +   =       100   ( set as 125 within fraction of second it is changed to 100)     

Vennila     Rs30     -   1   +   =        30

但如果我使用 getText,

resultTextView is nothing but in code I used as holder.cartPriceDum

resultTextview.getText().toString --> 返回 125 作为 ChocoBar 的项目值。

GetText 有效,但 setText 无效。搞砸了。请帮帮我,我花了更多时间修复它。但我还没有修复它。

MyCode如下:

holder.ivIncrease.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        clicked = true;

        cartPrice = Integer.parseInt(holder.cartPrice.getText().toString());
        InDeQuantity = Integer.parseInt(holder.cartCount.getText().toString());
        if (null != mListener) {
            cartRes.get(position).CartCount = (bigInt + 1);
            notifyItemChanged(position);
            mListener.onListFragmentInteraction(holder.cartRess);
        }
        UpdateCart(arg0);
    }

    private void UpdateCart(View view) {
        String pID = cartRes.get(position).product_id;
        int cartPrice = Integer.parseInt(holder.cartPrice.getText().toString());
        int cartIndividualTotal = cartRes.get(position).subTotal;

        int cartCount = cartRes.get(position).CartCount;

        System.out.println("Restaurant" + "   Check" + "Product ID  " + pID + "Cart Price " + cartPrice +

                " cartIndividual Total  " + cartIndividualTotal + " CartCount " + cartCount);

        CommonUtil.dbUtil.open();
        CommonUtil.dbUtil.updateaddToCart(pID, cartPrice, cartCount, cartIndividualTotal);
        Total = CommonUtil.dbUtil.getMultiply(pID);
        CommonUtil.dbUtil.updateNetAmount(pID, Total);
        setCartPrice(pID, holder);
        view.invalidate();

        /*--- Set all the updated value into the CartRes Class---*/

        System.out.println("Cart Details" + cartRes.get(position).product_id + "  " +
                cartRes.get(position).CartproductName + "  " + cartRes.get(position).CartcategoryName +
                " " + cartCount + "  " + cartPrice + "  " + Total);

        CartRes cartBasket = new CartRes(cartRes.get(position).product_id, cartRes.get(position).
                CartproductName, cartRes.get(position).CartcategoryName,
                cartCount, cartPrice, Total);
    }
});

/*======================================= *** ==============================================*/

/*--- If Cart count is 1, then disable Decrement icon*/
if (InDeQuantity <= 1) {
    holder.ivDecrease.setVisibility(View.INVISIBLE);
} else {
    holder.ivDecrease.setVisibility(View.VISIBLE);
}

/*====================================  Decreasing Cart ====================================*/

holder.ivDecrease.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        clicked = true;

        cartPrice = Integer.parseInt(holder.cartPrice.getText().toString());
        InDeQuantity = Integer.parseInt(holder.cartCount.getText().toString());
        if (null != mListener) {
            if (InDeQuantity > 1) {
                cartRes.get(position).CartCount = (bigInt - 1);
                notifyItemChanged(position);
                mListener.onListFragmentInteraction(holder.cartRess);
            } else if (InDeQuantity <= 0) {
                InDeQuantity = 1;
            }
        }
        UpdateCart(arg0);
    }

    private void UpdateCart(View view) {
        String pID = cartRes.get(position).product_id;
        int cartPrice = Integer.parseInt(holder.cartPrice.getText().toString());
        int cartCount = cartRes.get(position).CartCount;
        int cartIndividualTotal = cartRes.get(position).subTotal;


        CommonUtil.dbUtil.open();
        CommonUtil.dbUtil.updateaddToCart(pID, cartPrice, cartCount, cartIndividualTotal);
        System.out.println("Restaurant " + pID + "  " + cartPrice + " " + cartCount + " " + cartIndividualTotal);
        Total = CommonUtil.dbUtil.getMultiply(pID);

        CommonUtil.dbUtil.updateNetAmount(pID, Total);
        setCartPrice(pID, holder);

        view.invalidate();

        System.out.println("Cart Details" + cartRes.get(position).product_id + "  " +
                cartRes.get(position).CartproductName + "  " + cartRes.get(position).CartcategoryName +
                " " + cartCount + "  " + cartPrice + "  " + Total);

        CartRes cartBasket = new CartRes(cartRes.get(position).product_id,
                cartRes.get(position).CartproductName,
                cartRes.get(position).CartcategoryName,
                cartCount, cartPrice, Total);
    }
});

private void setCartPrice(String pID, ViewHolder holder) {
    Cursor cursor = CommonUtil.dbUtil.getCartIndividualPrice(pID);
    if (cursor != null && cursor.moveToFirst()) {
        int cartIndividualTotal = Integer.parseInt(cursor.getString(cursor.getColumnIndex(DbHelper.CART_TOTAL)));

        holder.cartPriceDum.setText(String.valueOf(cartIndividualTotal));

        Log.e("Restaurant", " ADapterCheck" + cartIndividualTotal + " " + holder.cartPriceDum.getText().toString());
    }
}

【问题讨论】:

    标签: android textview android-recyclerview


    【解决方案1】:

    您正在使您的观点无效。在setCartPrice(pID, holder); view.invalidate();之后

    每当您使视图无效时,视图将开始重绘自身,包括子视图。可能这就是它重置为默认值的原因。

    【讨论】:

    • 对不起,去掉view.invalidate()后结果还是一样;
    【解决方案2】:
    try like this
    
    
    
    //set Listener
    holder.ivIncrease.setOnClickListener(new IncreaseListener(holder.cartPrice,holder.cartCount,mListner,holder.cartRess,position));
    
    // Create listener like this
    class IncreaseListener implements View.OnClickListener
    {
    TextView cartPrice;
    TextView cartCount;
    Listener mListener;
    TextView cartRess;
    int position;
    IncreaseListener(TextView cartPrice,TextView cartCount,Listener mListener,TextView cartRess, int position)
    {
    this.cartPrice= cartPrice;
    this.cartCount = cartCount;
    this.cartRess = cartRess;
    this.position = position;
    this.mListener = mListener;
    }
        public void onClick(View arg0) {
            clicked = true;
    
           int  cartPrice = Integer.parseInt(cartPrice.getText().toString());
           int InDeQuantity = Integer.parseInt(cartCount.getText().toString());
            if (null != mListener) {
                cartRes.get(position).CartCount = (bigInt + 1);
                notifyItemChanged(position);
                mListener.onListFragmentInteraction(cartRess);
            }
            UpdateCart(arg0);
    
    }
    private void UpdateCart(View view) {
            String pID = cartRes.get(position).product_id;
            int cartPrice = Integer.parseInt(cartPrice.getText().toString());
            int cartIndividualTotal = cartRes.get(position).subTotal;
    
            int cartCount = cartRes.get(position).CartCount;
    
            System.out.println("Restaurant" + "   Check" + "Product ID  " + pID + "Cart Price " + cartPrice +
    
                    " cartIndividual Total  " + cartIndividualTotal + " CartCount " + cartCount);
    
            CommonUtil.dbUtil.open();
            CommonUtil.dbUtil.updateaddToCart(pID, cartPrice, cartCount, cartIndividualTotal);
            Total = CommonUtil.dbUtil.getMultiply(pID);
            CommonUtil.dbUtil.updateNetAmount(pID, Total);
            setCartPrice(pID, holder);
            view.invalidate();
    
            /*--- Set all the updated value into the CartRes Class---*/
    
            System.out.println("Cart Details" + cartRes.get(position).product_id + "  " +
                    cartRes.get(position).CartproductName + "  " + cartRes.get(position).CartcategoryName +
                    " " + cartCount + "  " + cartPrice + "  " + Total);
    
            CartRes cartBasket = new CartRes(cartRes.get(position).product_id, cartRes.get(position).
                    CartproductName, cartRes.get(position).CartcategoryName,
                    cartCount, cartPrice, Total);
        }
    }
    

    【讨论】:

    • 对不起结果是一样的。使用您的代码时未发现 Result 中的任何更改。 @Rohit Heera
    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    相关资源
    最近更新 更多