I hope someone help me for this. I need to implement a system of "cascading" alert dialogs. The user inserts a serie of information like as a flow, passing from a dialog to one other, storing data in an array. If a certain condition is verified, at the end of this flow i store array's data into a database.
Here is my code that could implement this:
this is the cycle which implements the flow, launched by an onMenuItemClick event
count=0;
do{
AlertDialog dialog;
dialog = alertSequence(view,label,edit);
dialog.show();
}while(count<4);
and this is the alertSequence()
function
public AlertDialog alertSequence(View v,TextView t,EditText e){
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
final EditText edit = e;
switch(count){
case 0:
builder.setView(v);
t.setText("Inserisci il titolo per il nuovo canto");
builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String elem = edit.getText().toString().trim();
if(elem.equals(null) || elem.length()==0){
// non faccio nulla
}
else{
info[count] = elem;
count++;
}
}
}).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
count = 10;
dialog.cancel();
}
});
break;
case 1:
builder.setView(v);
t.setText("Inserisci l'autore per il nuovo canto");
builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String elem = edit.getText().toString().trim();
if(elem.equals(null) || elem.length()==0){
// non faccio nulla
}
else{
info[count] = elem;
count++;
}
}
}).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
count = 10;
dialog.cancel();
}
});
break;
case 2:
builder.setMessage("Seleziona la categoria da assegnare");
int i=1;
String query = "SELECT titolo FROM categorie WHERE titolo<>'uncategorized' ORDER BY titolo ASC";
Cursor c = mydb.select(query);
if(!c.equals(null)){
options = new String[c.getCount()+1];
options[0] = "(annulla)";
while(c.moveToNext()){
options[i] = c.getString(0);
i++;
}
}
c.close();
mydb.close();
builder.setCancelable(false);
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(options[which].equals("(annulla)"))
count = 10;
else{
info[count] = options[which];
count++;
}
}
});
break;
case 3:
builder.setView(v);
t.setText("Inserisci il testo per il nuovo canto");
edit.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
edit.setHeight((int) 300d);
edit.setGravity(Gravity.LEFT | Gravity.TOP);
edit.setHorizontallyScrolling(false);
builder.setPositiveButton("Continua", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String elem = edit.getText().toString().trim();
if(elem.equals(null) || elem.length()==0){
// non faccio nulla
}
else{
info[count] = elem;
count++;
}
}
}).setNegativeButton("Annulla", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
count = 10;
dialog.cancel();
}
});
break;
}
return builder.create();
}
Is it correct for my scope? it is only that this generated a WindowManager$BadTokenException - Unable to add window token null is not for an application when i call the dialog.show()
in the do-while cycle. What could be? Thanks for help.
EDIT:
thanks blackbelt, i resolved it. But now there's another error, Illegalstateexception - The specified child already has a parent android you must call removeView()
from on the child's parent first on dialog.show()
. I have red something of this problem but i don't understand how resolve it. Any suggestions?