dimanche 10 janvier 2016

Android Studio: 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

Aucun commentaire:

Enregistrer un commentaire