0

我希望有人能帮助我。我需要实现一个“级联”警报对话框系统。用户插入一系列信息,如流,从对话传递到另一个对话,将数据存储在数组中。如果验证了某个条件,则在此流程结束时,我将数组的数据存储到数据库中。

这是我可以实现的代码:

这是实现流程的循环,由 onMenuItemClick 事件启动

count=0;
do{
        AlertDialog dialog;
        dialog = alertSequence(view,label,edit);
        dialog.show();
}while(count<4);

这就是alertSequence()功能

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();
}

它适合我的范围吗?只是这产生了一个WindowManager$BadTokenException -当我在 do-while 循环中调用时,无法添加窗口令牌 null 不适用于应用程序。dialog.show()可能是什么?感谢帮助。

编辑:
谢谢黑带,我解决了。但是现在还有另一个错误,Illegalstateexception - 指定的孩子已经有一个父 android,您必须首先 removeView() 从孩子的父母上调用dialog.show()。我有这个问题的红点,但我不明白如何解决它。有什么建议么?

4

1 回答 1

1

你得到那个异常是因为你需要创建对话框的上下文是活动的上下文

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

或者

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

如果你在一个片段内

于 2013-06-28T09:12:10.033 回答