简体   繁体   中英

My Dialog with a list doesn't work

I'm new on android studio and i have a litle problem. I want an dialog with some choices to select from an ArrayList (participantsShare in my code) but when i'm triying to open this dialog the app just stop, What am I doing wrong ?

final AlertDialog.Builder builder = new AlertDialog.Builder(Paiements.this);
builder.setTitle("Qui a payé ?");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Paiements.this, android.R.layout.simple_list_item_1, participantsShare );

builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int item) {
        participant.setText(participantsShare.get(item));
        dialog.cancel();
    }
});

AlertDialog dialog = builder.create();
dialog.show();

(participantsShare is an ArrayList<String> and participant is an EditText )

Use this code for showing an list in dialog.

AlertDialog.Builder builderSingle = new AlertDialog.Builder(DialogActivity.this);
builderSingle.setIcon(R.drawable.ic_launcher);
builderSingle.setTitle("Select One Name:-");

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(DialogActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.add("Hardik");
arrayAdapter.add("Archit");
arrayAdapter.add("Jignesh");
arrayAdapter.add("Umang");
arrayAdapter.add("Gatti");

builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String strName = arrayAdapter.getItem(which);
                AlertDialog.Builder builderInner = new AlertDialog.Builder(DialogActivity.this);
                builderInner.setMessage(strName);
                builderInner.setTitle("Your Selected Item is");
                builderInner.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,int which) {
                                dialog.dismiss();
                            }
                        });
                builderInner.show();
            }
        });
builderSingle.show();

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