将bundle传递给if语句

时间:2015-10-29 03:50:57

标签: java android if-statement bundle onactivityresult

活动声明---->有2 buttontextView(获得总金额)

活动项目1和汽油----->一个editText和一个保存button

活动声明

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

** Project1或Petrol,取决于......)**

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

是否可以将bundle传递给if statement?我想将值传递给另一个案例,最后总计两个值。我怎样才能做到这一点?现在我只获得值asbs,但不是值amount。我确实需要为bundle ...

添加if statement

继续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();

我提到How can i pass a string value, that has been created in an if statement, through a bundle inside another if statement?,但答案对我来说还不太清楚。

索赔的图像

![enter image description here

吐司应该显示1 12 13但我得到1 12 12。似乎无法添加值1

1 个答案:

答案 0 :(得分:0)

您应该将您在onActivityResult中传递的值存储在Activity的变量中,然后直接读取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();
    }
}

您仍然需要根据您的标准初始化您的值。