JStephen
JStephen

Reputation: 1149

Is it possible to build a multichoice Alert Dialog from an adapter?

I have an AlertDialog setup almost identical to the one described in this link: https://stackoverflow.com/a/4697761/2593088
I use it for pulling photos from an SD card, so i have the photo and the name next to it in the list. It works great if I only need to pull one photo at a time, but it would be nicer if I could just select all the photos and pull them over all at once. The problem is as soon as one is clicked the dialog closes. I have searched ways around this but each way I've found doesn't work for what I need. One way was to overwrite the onClick listener in the dialogs OnShow listener but I could only figure how to overwrite the positive/negative/neutral buttons, not the list items. I have also been able to make it work without the icons using the setMultiChoiceItems when building the dialog, so if there is a simple way to just add icons at this point that would work too. I'm just wondering if there is a simple way to do it with what I have or if I have to write my own customer adapter or somthing.
Any help is Appreciated.

EDIT: here is what i have that works fine except there are no icons.

dialog = builder.setAdapter(adapter, null).setMultiChoiceItems(mFileList, null, new OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface arg0, int arg1,
            boolean arg2) {


        String selectedFile = mFileList[arg1];
        if(selectedFile.equals("Up One Folder")) selectedFile = "..";
        String mChosenFile = tempDir + "/" + selectedFile;
        File f = new File(mChosenFile);
        if(f.isDirectory()) {
            // it is a directory, clear all selections and go to the next directory

            selectedImages.clear();

            dialog.dismiss();
            loadFileList(mChosenFile);
            getFiles(false);


        } else {
            if(arg2) {
                selectedImages.add(tempDir + "/" + selectedFile);
            }
        }


    }

})

I was hoping that using both setAdapter and setMultiChoiceItems would work but setting the adapter does nothing if the multichoice items is set. And unfotunately setMultiChoiceItems doesn't have a constructor that uses an adapter.

Upvotes: 0

Views: 634

Answers (2)

JStephen
JStephen

Reputation: 1149

I ended up having to write my own custom dialog instead of using setMultiChoiceItems. I should have known I would have to do it this way from the moment I noticed there was no multi-choice constructor with an adapter. I was just hoping there was a way around it because the default list dialog can be built with an adapter. Hopefully this changes in the future.

Upvotes: 1

Husnain Iqbal
Husnain Iqbal

Reputation: 466

*I actually never used the adapter with Alert Dialog but I have used this way to implement multichoice alert dialog:

LinearLayout settingLayout = new LinearLayout(MainActivity.this) ;
settingLayout.setOrientation(LinearLayout.VERTICAL) ;

final RadioButton radOptionTen = new RadioButton(MainActivity.this) ;
final RadioButton radOptionTwenty = new RadioButton(MainActivity.this) ;
final RadioButton radOptionThirty = new RadioButton(MainActivity.this) ;
final RadioButton radOptionFifty = new RadioButton(MainActivity.this) ;
final RadioButton radOptionHundred = new RadioButton(MainActivity.this) ;
final RadioButton radOptionTwoHundred = new RadioButton(MainActivity.this) ;

radOptionTen.setText("10") ;
radOptionTwenty.setText("20") ;
radOptionThirty.setText("30") ;
radOptionFifty.setText("50") ;
radOptionHundred.setText("100") ;
radOptionTwoHundred.setText("200") ;

radOptionTen.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }

}) ;

radOptionTwenty.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
}) ;

radOptionThirty.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}) ;

radOptionFifty.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
          // TODO Auto-generated method stub

    }

}) ;

radOptionHundred.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
          // TODO Auto-generated method stub

}

}) ;

radOptionTwoHundred.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    }

}) ;

settingLayout.addView(radOptionTen) ;
settingLayout.addView(radOptionTwenty) ;
settingLayout.addView(radOptionThirty) ;
settingLayout.addView(radOptionFifty) ;             
settingLayout.addView(radOptionHundred) ;
settingLayout.addView(radOptionTwoHundred) ;

new AlertDialog.Builder(UserSettingsActivity.this)
.setIcon(R.drawable.alert_icon)
.setTitle("Title!")
.setMessage("Message...")
.setView(settingLayout) 
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
    }
}) 
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

    }
}) 
.show() 

You can remove the positive and negative buttons of dialog and Change the layout or widgets that are required to you. I think, in your case, you want to use buttons, so replace the radio-buttons with buttons and remove the dialog buttons

Upvotes: 0

Related Questions