1

package com.corporation.ilumian.latihandua;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {

    Button btnSubmit;
    EditText txtNama,txtKelas;
    RadioGroup radGroup;
    RadioButton radBut1,radBut2;
    CheckBox boxBola,boxGame,boxMakan;
    String cbBola,cbGame,cbMakan;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSubmit = (Button)findViewById(R.id.button);
        txtNama = (EditText)findViewById(R.id.editText);
        txtKelas = (EditText)findViewById(R.id.editText2);
        radGroup = (RadioGroup)findViewById(R.id.radioGroup);
        boxBola = (CheckBox)findViewById(R.id.checkBox);
        boxGame = (CheckBox)findViewById(R.id.checkBox2);
        boxMakan = (CheckBox)findViewById(R.id.checkBox3);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();

            }
        });
    }

    public void showDialog(){
        int selectedRb = radGroup.getCheckedRadioButtonId();
        radBut1 = (RadioButton)findViewById(selectedRb);
        getCheckboxData();
        AlertDialog.Builder hasilInput = new AlertDialog.Builder(this);


        //set judul
        hasilInput.setTitle("Hasil Input");
        hasilInput.setMessage("Nama : " + txtNama.getText().toString() + "\n" +
                              "Kelas : " + txtKelas.getText().toString() + "\n" +
                              "Kamu Seorang " + radBut1.getText() + "\n" +
                              "Hobby : " + cbBola + cbGame + cbMakan + "\n").setPositiveButton("Oke", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog alert = hasilInput.create();
        alert.show();




    }

    public void getCheckboxData(){

        if (boxBola.isChecked() && boxGame.isChecked() && boxMakan.isChecked()){
            cbBola = boxBola.getText().toString() + ", ";
            cbGame = boxGame.getText().toString() + ", ";
            cbMakan = boxMakan.getText().toString();
        }
        else {
            if (boxBola.isChecked() && boxGame.isChecked()){
                cbBola = boxBola.getText().toString() + ", ";
                cbGame = boxGame.getText().toString();
            }
            else{
                if (boxBola.isChecked() && boxMakan.isChecked()){
                    cbBola = boxBola.getText().toString() + ", ";
                    cbMakan = boxMakan.getText().toString();
                }
                else {
                    if (boxBola.isChecked()){
                        cbBola = boxBola.getText().toString();
                    }
                    else {
                        cbBola = "";
                    }
                }
            }
        }


    }
}

Hello everyone thank you for your time reading my first post I've homework about adding comma automatically on the text in the alert dialogue here is the app look like

the "UI" this is where the alert dialogue take the information

Here is the example when submit button is pressed

now the problem is how to add comma between text on hobby?

before ( the result in the picture )

Hobby : Sepak Bola Main Game Makan

no comma to separate the hobby

here how the alert dialogue should look like automatically when 2 or more checkbox is selected

Hobby : Sepak Bola, Main Game, Makan or Hobby : Sepak Bola, Makan

how to add this comma? without manually typing ...(" , ".. in the code

Sorry if this type of question is already been discussed, I genuinely don't know what keyword to search on google or StackOverflow

Thank you

1 Answers1

1

You can use guava joiner to join texts. But I have no idea about data structure what you used to save this data. If you use the list to save hobbies, u can do it like below

List<String> hobbies= Lists.newArrayList("Sepak Bola", " Main Game", "Makan");
String result = Joiner.on(",").join(hobbies);

Tim's edit: We can use the above appraoch with your actual dialog code:

List<String> hobbies = Lists.newArrayList(cbBola, cbGame, cbMakan);
String result = Joiner.on(",").join(hobbies);

hasilInput.setMessage("Nama : " + txtNama.getText().toString() + "\n" +
                      "Kelas : " + txtKelas.getText().toString() + "\n" +
                      "Kamu Seorang " + radBut1.getText() + "\n" +
                      "Hobby : " + result  + "\n")
    .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
});
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23