Hi,
I have created the MultiChoice AlertDialog The AlertDialog has five list items with checkboxes. When I check First checkbox, w.r.t this the if the other checkboxes in the list are checked they shud be unchecked automatically and vice versa.
I am checking the isChecked status in the onClick method of OnMultiChoiceClickListener() and calling the showDialog(DIALOG_MULTIPLE_CHOICE); by updating boolean[] checkedItems; to recreate the Dialog, But I am unable to achieve it. If you any suggestions please direct me to right way.
Is there any way to recreate the AleartDialog onClick event of the radio button click.
Some Sample Code below:
case DIALOG_MULTIPLE_CHOICE:
final String[] lJobTypes = { "Item1", "Item2", "Item3","Item4", "Item5" };
return new AlertDialog.Builder(JoblistPage.this)
// .setIcon(R.drawable.logo)
.setTitle("Title Here")
// .setCustomTitle(m_Title)
.setMultiChoiceItems(lTypes, m_Selections,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,int whichButton, boolean isChecked) {
/* User clicked on a check box do some stuff */
if (isChecked) {
m_CheckCount++;
//Toggle the Radio button Check status
} else {
m_CheckCount--;
}
}
}).setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
Regards Vinayak
AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.
Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.
AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .
Don't recreate the dialog, just toggle the checkboxes within the current dialog. Your onMultiChoiceClickListener can keep track of the currently active checkbox (if any) and uncheck it when another is selected. Here's a complete tested, working example:
package com.stackoverflow.beekeeper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
public class StackOverflowTest extends Activity {
/** Called when the activity is first created. */
@Override public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private int mSelected = -1;
@Override protected void onResume() {
super.onResume();
final Builder build = new Builder(this);
build.setTitle("List selection");
build.setCancelable(true);
final String[] strings = new String[]{"Cow", "Horse", "Goat"};
final OnMultiChoiceClickListener onClick =
new OnMultiChoiceClickListener() {
@Override public void onClick(final DialogInterface dialog,
final int which, final boolean isChecked) {
if (isChecked) {
if ((mSelected != -1) && (mSelected != which)) {
final int oldVal = mSelected;
final AlertDialog alert = (AlertDialog)dialog;
final ListView list = alert.getListView();
list.setItemChecked(oldVal, false);
}
mSelected = which;
} else
mSelected = -1;
}
};
build.setMultiChoiceItems(strings, null, onClick);
build.setPositiveButton("Done", new OnClickListener() {
@Override public void onClick(final DialogInterface dialog,
final int which) {
String message = null;
if (mSelected == -1)
message = "You didn't select anything.";
else
message = "You selected '" + strings[mSelected] + "'";
Toast.makeText(StackOverflowTest.this, message, Toast.LENGTH_LONG).show();
}
});
build.show();
}
}
One thing to watch for: you must specify "null" for the "checkedItems" parameter in your "setMultiChoiceItems" call -- otherwise the "setItemChecked" calls won't work as expected. It would end up using that array to store the checked state, and "setItemChecked" would'nt update it correctly, so everything would get confused. Odd, but true.
I was struggling with this for quite some time. I maintain an array of the "checked" status of each of the items and change that value while visually changing the setItemChecked value. Then when the done button is clicked I iterate through "checked" to save the values to my db.
builder.setMultiChoiceItems(cats, checked, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean check) {
if(cats[item].equals("All Categories")) {
AlertDialog d = (AlertDialog) dialog;
ListView v = d.getListView();
int i = 0;
while(i < cats.length) {
v.setItemChecked(i, check);
checked[i] = check;
i++;
}
}
checked[item] = check;
}
});
Did you try replacing setMultiChoiceItems
to setSingleChoiceItems
in your dialog?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With