Search code examples
javaandroidif-statementbundleonactivityresult

Pass bundle to if-statement


Activity Claims----> has 2 button and a textView (get the total amount)

Activity Project1 and Petrol-----> one editText and a save button

Activity Claims

 button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogRadio(a1);

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialogRadio(a2);

            }
        });

 public void AlertDialogRadio(final int k) {
         final CharSequence[] ClaimsModel = {"Project1", "Petrol"};

        AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
        alt_bld.setTitle("Select a Claims");
        alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
                .OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {
                if (item == 0) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
                   intent.putExtra("k",k); 
                    startActivityForResult(intent, 0);
                } else if (item == 1) {
                    Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
                   intent.putExtra("k", k);
                    startActivityForResult(intent, 1);
                }

** Either Project1 or Petrol, depends...)**

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.project);
       text= (EditText)findViewById(R.id.editText1);
        Button save=(Button)findViewById(R.id.button12);

        save.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent returnIntent = new Intent();
                l="A";
                text = text.getText().toString();
                returnIntent.putExtra("text", text);
                returnIntent.putExtra("l", l);
                final int k1 = getIntent().getExtras().getInt("k");
                returnIntent.putExtra("k1", k1);
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

    }

    @Override
    public void onBackPressed()
    {
        Intent returnIntent = new Intent();
        setResult(Activity.RESULT_CANCELED, returnIntent);
        finish();
    }

Is it possible to pass a bundle to if statement? I want to pass a value to another cases and finally total up the two values. How can I achieve this? Now I only get the value as and bs, but not value amount. I do believe it need to add a bundle to both if statement...

Continue Claims.java....

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            int button = data.getIntExtra("k1", 0);  // to check which button was pressed
            long a=0;
            long as=0;
            long bs=0;
            String result;
            String result1;

            if (button == 1) {
                switch (requestCode) {
                    case 0:
                        result = data.getStringExtra("text");
                        String b = data.getStringExtra("l");
                         as=Long.parseLong(result);
                        c.setText("            " + b + "------" + "RM " + result);
                        Toast.makeText(getActivity(),as+"", Toast.LENGTH_LONG).show();
                        break;

                    case 1:
                         result = data.getStringExtra("text");
                        String b1 = data.getStringExtra("l");
                         as=Long.parseLong(result);
                        c.setText("            " + b1 + "------" + "RM " + result);
                        Toast.makeText(getActivity(),as+"", Toast.LENGTH_LONG).show();
                        break;


            }
             if(button==2)
            {
                switch (requestCode) {
                    case 0:
                        result1 = data.getStringExtra("text");
                        String b = data.getStringExtra("l");
                         bs=Long.parseLong(result1);
                        d.setText("            " + b + "------" + "RM " + result1);
                        Toast.makeText(getActivity(),bs+"", Toast.LENGTH_LONG).show();
                        break;

                    case 1:
                        result1 = data.getStringExtra("text");
                        String b1 = data.getStringExtra("l");
                         bs=Long.parseLong(result1);
                        d.setText("            " + b1 + "------" + "RM " + result1);
                        Toast.makeText(getActivity(),bs+"", Toast.LENGTH_LONG).show();
                        break;

                }

            }

      else if(requestCode==CAMERA_REQUEST_CODE)
            {

            }

            long amount=as+bs;
            Toast.makeText(getActivity(),amount+"", Toast.LENGTH_LONG).show();

I refer How can i pass a string value, that has been created in an if statement, through a bundle inside another if statement? but the answer is quite unclear for me.

The image for Claims

![enter image description here

The toast should display 1 12 13 but I get 1 12 12. It seems like the value 1 cannot be added!


Solution

  • You should store the values you get passed in your onActivityResult in a variable of your Activity, and then directly read these variables for the Toast.

    public class Claims{
        long a, as, bs;
    
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            int button = data.getIntExtra("k1", 0);
            String result;
    
            if (button == 1) {
                result = data.getStringExtra("text");
                as=Long.parseLong(result);
            } else if(button==2) {
                result = data.getStringExtra("text");
                bs=Long.parseLong(result1);
            }
    
            long amount=as+bs;
            Toast.makeText(getActivity(),amount+"", Toast.LENGTH_LONG).show();
        }
    }
    

    You still need to initialise your values according to your standards though.