Search code examples
javaandroidtoastonactivityresult

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.

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. Between, the amount I get is incorrect.

Sorry if it comes across as a silly question....


Solution

  • Try this:

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