0

I'm trying to create an AlertDialog (which should appear when the Textvie exercise is clicked), which includes a spinner as title. It should change the content of the dialog list, when a diffrent item is selected in the spinner.

exercise = (TextView)findViewById(R.id.add_exerc);
exercise.setText("TEST");
initExerOCL();  //init the OnItem

private void initExerOCL(){
    exercise.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //diaTitle is used for the String[] from which to create the dialogList
            AlertDialog a = showListCheckPickerDialog(diaTitle);
            a.show();
        }
    });
}

public AlertDialog showListCheckPickerDialog(int i){
    mSelectedItems = new ArrayList();  //saves selected items

    LayoutInflater inflater = getLayoutInflater();
    View v = inflater.inflate(R.layout.add_dia_spinner_title, null);
    s = (Spinner) findViewById(R.id.add_dia_t);
    ArrayAdapter<CharSequence> adapterS = ArrayAdapter.createFromResource(this,
            R.array.trainings, R.layout.spinner_item);
    adapterS.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapterS);
    s.setOnItemSelectedListener(this);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.a_add)
    //set the View for the spinner
    .setCustomTitle(v)
    .setMultiChoiceItems(diaTitle, null,
            new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which,
                boolean isChecked) {
            if (isChecked) {
                // If the user checked the item, add it to the selected items
                mSelectedItems.add(which);
            } else if (mSelectedItems.contains(which)) {
                // Else, if the item is already in the array, remove it 
                mSelectedItems.remove(Integer.valueOf(which));
            }
        }
    })
    // Set the action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    });
    return builder.create();
}

I hope someone nows a solution or a better way, because this way seem quite complicated for me (and does not even work). Thanks!

insch
  • 61
  • 1
  • 1
  • 9

2 Answers2

0

Use an Activity instead of a Dialog see this post: Android Activity as a dialog or this part of the docs: http://developer.android.com/guide/topics/ui/dialogs.html#ActivityAsDialog.

Then to implement the spinner, setup dropdown navigation inside your activity with the contents you want (see the docs for details http://developer.android.com/guide/topics/ui/actionbar.html#Dropdown)

Community
  • 1
  • 1
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40
0

Instead of inflating a layout just create a Spinner programatically and add it to your custom title.

sample:

Spinner s2 = new Spinner(this);
    ArrayAdapter<CharSequence> adapterS = ArrayAdapter.createFromResource(this,
            R.array.trainings, R.layout.spinner_item);
    adapterS.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapterS);
    s.setOnItemSelectedListener(this);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("hello")
    //set the View for the spinner
    .setCustomTitle(s2)
    // Set the action buttons
    .setPositiveButton("okie", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })
    .setNegativeButton("lawl", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) { }
    })
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63