简体   繁体   中英

Android: How to use if statement for bundle/intent values?

so a quick question. In my app, the users go through multiple activities that provides them with radio-buttons to choose from.. at the final activity, based on there options, the will be shown which character they are etc... Now the problem is I don't know how to write a code in order to do that. Here is what I have

First activity

public class Quiz1 extends Activity {

Button btn;
RadioGroup rg1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz1);

    btn = (Button) findViewById(R.id.nextBtn1);
    rg1= (RadioGroup) findViewById(R.id.rg1);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg1.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent = new Intent(getApplicationContext(), Quiz2.class);
                Bundle bundle = getIntent().getExtras();

                int id = rg1.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg1", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

Second activity

Button btn;
RadioGroup rg2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quiz2);


    btn = (Button) findViewById(R.id.nextBtn2);
    rg2= (RadioGroup) findViewById(R.id.rg2);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (rg2.getCheckedRadioButtonId() == -1) {

                Toast.makeText(getApplicationContext(), "Please select an answer",
                        Toast.LENGTH_SHORT).show();
            } else{
                Intent intent = new Intent(getApplicationContext(), Quiz3.class);
                Bundle bundle = getIntent().getExtras();
                int id = rg2.getCheckedRadioButtonId();
                RadioButton radioButton = (RadioButton) findViewById(id);
                bundle.putString("rg2", radioButton.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }

        }

    });
  }
}

This continues for about 7 activities

Final activity (where the result and the character are shown)

public class Final1 extends Activity {

Button btnRestart;
Button btnShare;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView1.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView2.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView3.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView4.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView5.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView6.setText(bundle.getCharSequence("rg6"));

    btnRestart = (Button)findViewById(R.id.restartBtn);

    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

    btnShare = (Button)findViewById(R.id.btnShare);

    btnShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "check out this app";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));


        }
    });


  }

}

Now, in the final activity, I want to have a code where if for example:

if in activity1(Quiz1) options 1 OR 2 are chosen ( as in the radio-buttons selected),

Quiz2: options 2 or 4

Quiz3: options 1 or 4

Quiz2: options 2 or 3

and so on...

then change a textview to something specific like "your character is x"

I have already carried all the information to the final class, I just don't know how to approach this problem, even-though it sounds simple.

Any help would be appreciated, thank you <3

EDIT:

TextView textviewResult = (TextView) findViewById(R.id.textViewResult);
    if(bundle.getString("rg").equals("A")||(bundle.getString("rg").equals("B")&& bundle.getString("rg1").equals("B")&& bundle.getString("rg2").equals("Long range weapons")
            && bundle.getString("rg3").equals("C") || bundle.getString("rg3").equals("D") && bundle.getString("rg4").equals("A")||bundle.getString("rg4").equals("B")
            || bundle.getString("rg4").equals("CC") && bundle.getString("rg5").equals("A") || bundle.getString("rg5").equals("E")
            && bundle.getString("rg6").equals("Yes"))) {
        textviewResult.setText("x");

    }else{
        textviewResult.setText("not x");
    }

The problem with this is, even if I choose another option for rg (so not the "A" or "B" options), but then for the rest I choose the ones in the If statement, it still ends up saying x(but it should be saying Not x)

You can use the String equals() method inside your if statement. It returns true if both Strings are equal.

Eg:

if(bundle.getString("rg").equals("YourRadioButtonText")){ ... }

Your code could look something like this:

    `public class Final1 extends Activity {

Button btnRestart;
Button btnShare;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.final1);

    Bundle bundle = getIntent().getExtras();

    TextView textView = (TextView)findViewById(R.id.txt);
    textView.setText(bundle.getCharSequence("rg"));

    TextView textView1 = (TextView)findViewById(R.id.txt1);
    textView1.setText(bundle.getCharSequence("rg1"));

    TextView textView2 = (TextView)findViewById(R.id.txt2);
    textView2.setText(bundle.getCharSequence("rg2"));

    TextView textView3 = (TextView)findViewById(R.id.txt3);
    textView3.setText(bundle.getCharSequence("rg3"));

    TextView textView4 = (TextView)findViewById(R.id.txt4);
    textView4.setText(bundle.getCharSequence("rg4"));

    TextView textView5 = (TextView)findViewById(R.id.txt5);
    textView5.setText(bundle.getCharSequence("rg5"));

    TextView textView6 = (TextView)findViewById(R.id.txt6);
    textView6.setText(bundle.getCharSequence("rg6"));

    // NEW

TextView textviewResult = (TextView) findViewById(R.id.resultTV);
if(bundle.getString("rg").equals("C")){
        textviewResult.setText("It is C");
}
else if(bundle.getString("rg1").equals("A") || bundle.getString("rg2").equals("B")){
        textviewResult.setText("It is A or B");
}
else if(bundle.getString("rg1").equals("C") && bundle.getString("rg2").equals("D")){
        textviewResult.setText("It is C and D");
}
else if(!bundle.getString("rg1").equals("A")){
        textviewResult.setText("It is not A");
}
else {
    textviewResult.setText("Mhhh");
}

// END



    btnRestart = (Button)findViewById(R.id.restartBtn);

    btnRestart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(v.getContext(), Quiz.class);
            startActivityForResult(in, 0);
        }
    });

    btnShare = (Button)findViewById(R.id.btnShare);

    btnShare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            String shareBody = "check out this app";
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent, "Share via"));


        }
    });


  }

}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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