setSingleChoiceItems and setPositiveButton Android

2.5k views Asked by At

Is there a way to turn multiple check box into box that can have only one box checked at time?

So here is something I am trying to solve, with dialog alertbox and singlechoice-multichoice.

public class MainActivity extends Activity {

    CharSequence[] items = {"FIT", "Fakultet za menadzment", "Fakultet digitalnih umetnosti"};
    private String selectedItem;
    public int proba;


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

    }

    private void launchWebPage(String url){
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }

    public void dialog(View v){
        showDialog(0);
    }

    public void dialog1(View v){
        Toast.makeText(getBaseContext(), "Nema deklarisanu funkciju.", Toast.LENGTH_LONG).show();
    }

    public void dialog2(View v){
        Toast.makeText(getBaseContext(), "Nema deklarisanu funkciju.", Toast.LENGTH_LONG).show();
    }


    public Dialog onCreateDialog(final int id) {

                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Odabir");
                builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                selectedItem = (String) items[i];
                                proba = i;


                            }
                        });
                        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int position) {

                                Toast.makeText(getBaseContext(), "Test", Toast.LENGTH_SHORT).show();

                            }
                        });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getBaseContext(), "Cancel pritisnuto!", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.show();

        return null;
    }
}
3

There are 3 answers

0
mattfred On BEST ANSWER
0
Beansolder On

Finally figured it out, if somebody needs something similar to this, this is the missing code.

public Dialog onCreateDialog(final int id) {

            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Odabir");
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            selectedItem = (String) items[i];
                            proba = i;

                        }
                    });
                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int position) {

                            ListView lw = ((AlertDialog) dialog).getListView();
                            Object checkedItem = lw.getAdapter().getItem(lw.getCheckedItemPosition());
                            Toast.makeText(getApplicationContext(), ((String) checkedItem), Toast.LENGTH_SHORT).show();

                            if(proba == 0){
                                launchWebPage("http://www.metropolitan.ac.rs/osnovne-studije/fakultet-informacionih-tehnologija/");
                            } else if(proba == 1){
                                launchWebPage("http://www.metropolitan.ac.rs/osnovne-studije/fakultet-za-menadzment/");
                            } else {
                                launchWebPage("http://www.metropolitan.ac.rs/fakultet-digitalnih-umetnosti-2/");
                            }

                        }
                    });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(getBaseContext(), "Cancel pritisnuto!", Toast.LENGTH_SHORT).show();
                }
            });

            builder.show();

    return null;
}
3
CommonsWare On

Use one of the varieties of setSingleChoiceItems() on AlertDialog.Builder. Despite what the documentation says, this should use radio buttons, not checkboxes.