简体   繁体   English

计算总金额

[英]Calculate the total amount

I'm trying to get the total amount and display it as Toast, but I get error as below...I have converted the String to Long by using Long.parseLong() but still no luck. 我正在尝试获取总量并将其显示为Toast,但出现以下错误...我已经通过使用Long.parseLong()String转换为Long了,但是还是没有运气。

Error:(247, 18) error: no suitable method found for makeText(Context,long,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; long cannot be converted to CharSequence)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; possible lossy conversion from long to int)    

Code

 @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            int button = data.getIntExtra("k1", 0);
            long a=0,long c1=0;
            long as=0,long as1=0,long as2=0;
            long bs=0,bs1=0,bs2=0;
            if (button == 1) {
                switch (requestCode) {
                    case 0:
                        String result = data.getStringExtra("text"); //Assume it holds 35
                        String b = data.getStringExtra("a");
                         as=Long.parseLong(result);
                        c.setText("            " + b + "------" + "RM " + result);
                        break;

                    case 1:
                        String result1 = data.getStringExtra("text");
                        String b1 = data.getStringExtra("a");
                         as1=Long.parseLong(result1);
                        c.setText("            " + b1 + "------" + "RM " + result1);
                        break;


                    case 2:
                        String result2 = data.getStringExtra("text");
                        String b2 = data.getStringExtra("a");
                         as2=Long.parseLong(result2);
                        c.setText("            " + b2 + "------" + "RM " + result2);
                        break;

                }
            }
            else if(button==2)
            {
                switch (requestCode) {
                    case 0:
                        String result = data.getStringExtra("text");
                        String b = data.getStringExtra("a");
                         bs=Long.parseLong(result);
                        d.setText("            " + b + "------" + "RM " + result);
                        break;

                    case 1:
                        String result1 = data.getStringExtra("text");
                        String b1 = data.getStringExtra("a");
                         bs1=Long.parseLong(result1);
                        d.setText("            " + b1 + "------" + "RM " + result1);
                        break;


                    case 2:
                        String result2 = data.getStringExtra("text"); //Assume it holds 40
                        String b2 = data.getStringExtra("a");
                         bs2=Long.parseLong(result2);
                        d.setText("            " + b2 + "------" + "RM " + result2);
                        break;

                }

                long x=as+as1+as2;
                long y=bs+bs1+bs2;
                long amount=x+y;
                Toast.makeText(getActivity().getApplicationContext(), amount, Toast.LENGTH_LONG).show();
            }

Do it has another way to write this since I only need to get 1 value from each button. 它有另一种写此方法的方法,因为我只需要从每个按钮获取1个值即可。 Between, the amount I get is incorrect. 之间,我得到的金额不正确。

Sorry if it comes across as a silly question.... 抱歉,这是一个愚蠢的问题。

尝试这个:

Toast.makeText(getActivity().getApplicationContext(), String.valueOf(amount), Toast.LENGTH_LONG).show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM