Как переместить данные с RaddioButton к другому виду деятельности - PullRequest
0 голосов
/ 07 марта 2020

Я хочу, чтобы, если бы я выбрал один из своих RadioButton, он показывал String в другом упражнении, как это сделать? это застряло в моей учебе для моей школы, если у вас есть лучший способ, который может мне помочь, в другой сети, всегда использующей Toast, например RadioButton, который не может помочь мне полностью

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dateFormatter = new  SimpleDateFormat ("dd-MM-yyyy", Locale.US);

    tvDataResult = (TextView) findViewById(R.id.tvSelectedDate);
    btDataPicker = (Button) findViewById(R.id.btndate);

    btDataPicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showDateDialog();
        }
    });

    Spinner spinner = findViewById(R.id.planets_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.planets_array,android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    final EditText edtNama = (EditText)findViewById(R.id.etNama);
    final EditText edtNim =(EditText)findViewById(R.id.etNim);
    tvDataResult = (TextView)findViewById(R.id.tvSelectedDate);
    Button button = (Button)findViewById(R.id.btnTampilkan);

    final RadioGroup rbg = (RadioGroup)findViewById(R.id.rgroup);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String nama = edtNama.getText().toString();
            String nim = edtNim.getText().toString();
            String date = tvDataResult.getText().toString();

            int selectedId = rbg.getCheckedRadioButtonId();
            rblaki=(RadioButton)findViewById(selectedId);
            rbcewe=(RadioButton)findViewById(selectedId);

            Intent intent = new Intent(MainActivity.this,ResultActivity.class);
            intent.putExtra("Nama",nama);
            intent.putExtra("NIM",nim);
            intent.putExtra("tanggal",date);

            startActivity(intent);
        }
    });
}

Это мой XML. я только показываю это

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Jenis Kelamin"
    android:textSize="17dp" />

<RadioGroup
    android:id="@+id/rgroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/rblaki"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Laki-laki" />

    <RadioButton
        android:id="@+id/rbcewe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Perempuan" />
</RadioGroup>


<Button
    android:id="@+id/btnTampilkan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TAMPILKAN" />

<Button
    android:id="@+id/btnDelete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="DELETE" />

Я хочу быть таким

1 Ответ

1 голос
/ 07 марта 2020

Я думаю, что лучшая практика для этого - создание класса, реализующего Serializable для передачи в другое действие или фрагмент с использованием дополнительных функций Bundle (способ передачи объекта на другой экран).

Шаг 1 - Пример объекта: не забудьте добавить get и setters

   public class Person implements Serializable { 
      private String nama; 
      private String nim;
      private String date;
  }

Шаг 2 - Передача объекта другому действию

    ....
    Intent intent = new Intent(MainActivity.this, ResultActivity.class);
    intent.putExtra("objParameter", obj); // obj is your class serializable
    startActivity(intent);
  }

Шаг 3. Получение объекта в ResultActivity

      //Inside onCreate() method
      Person personParam = (Person) getIntent().getSerializableExtra("objParameter");

Примечания. Убедитесь, что вы реализуете Serializable в своем классе или внутреннем классе:)

...