TextView中的匿名错误

时间:2016-05-25 06:11:13

标签: android textview android-recyclerview

我正在开发一个餐厅应用程序。在这里,我使用购物车页面查看用户选择结帐的食品。购物车项目使用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());
    }
}

2 个答案:

答案 0 :(得分:0)

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

之后

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

答案 1 :(得分:0)

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