简体   繁体   中英

how to display the clicked radio button value on my second activity in android studio?

This is my java code:

public class jenis extends AppCompatActivity {

    public Button button3;
    RadioGroup radioGroup2;
    RadioButton radioButton2;
    DatabaseReference databaseReference;

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

        radioGroup2 = (RadioGroup) findViewById(R.id.radioGroup2);
        databaseReference = FirebaseDatabase.getInstance().getReference();

        final RadioButton stock = (RadioButton)findViewById(R.id.radioButton11);
        final RadioButton property = (RadioButton)findViewById(R.id.radioButton12);

        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (stock.isChecked()){
                    Variable newVar = new Variable();
                    newVar.setAmountt("jeniss");
                    databaseReference.child(newVar.getAmountt()).setValue("Stock Waqf");
                }

                else if(property.isChecked()){
                    Variable newVar = new Variable();
                    newVar.setAmountt("jeniss");
                    databaseReference.child(newVar.getAmountt()).setValue("Property Waqf");
                }

                if(stock.isChecked()){
                    Intent intent = new Intent(jenis.this, stock.class);
                    startActivity(intent);
                }

                if(property.isChecked()){
                    Intent intent = new Intent(jenis.this, property.class);
                    startActivity(intent);
                }
            }
        });
    }

}

How can I display the clicked radio button value on my second activity?

I really appreciate your help, thank you.

pass the value as intent

 Intent intent = new Intent(youractivity.this,secondactivity.class);
 intent.putExtra("key",radiobutton.getText());
 startActivity(intent);

to access it on second activity

in OnCreate() method
    getIntent().getStringExtra("key");

or else

 you can also store your radiobutton checked value in shared preference and 
  then access it on another activity... It will work like session
public class stock extends AppCompatActivity {
DatabaseReference databaseReference;


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

    final TextView textViewOut = (TextView) findViewById(R.id.textViewOut2);


    textViewOut.setText(" ");


}

public void OnCreate() {
    getIntent().getStringExtra("jeniss");
   }

}

what did i missed ?

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