android:在onCreateDialog内调用对话框重绘

问题描述

| 我在对话框布局上放置了一个按钮,希望触发对话框的完全重绘。但是,看来您无法从onCreateDialog内打开对话框,而且我找不到不使用
setPositiveButton
之类的方法关闭对话框,因为这是唯一将对话框作为参数的Button覆盖(而且我无法找到一种方法获取属于布局一部分的Button)。这是所有相关代码
case DIALOG_WIFI_PREF:
{
    Dialog dialog = new Dialog(this);
    final View dialogLayout = inflater.inflate(R.layout.dialog_wifi_pref,null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialogLayout);
    builder.setTitle(\"IP Configuration\");
    final EditText ipIn = (EditText)dialogLayout.findViewById(R.id.wifi_ip_in);
    final EditText portIn = (EditText)dialogLayout.findViewById(R.id.wifi_port_in);
    final EditText labelIn = (EditText)dialogLayout.findViewById(R.id.site_label_in);
    final EditText codeIn = (EditText)dialogLayout.findViewById(R.id.activation_code);
    final Spinner siteSpn = (Spinner)dialogLayout.findViewById(R.id.site_spn);
    final Button deleteBtn = (Button)dialogLayout.findViewById(R.id.delete_btn);

    final Cursor cur = mDb.rawQuery(\"SELECT * FROM \" + DbSchema.SiteSchema.TABLE_NAME,null);
    cur.movetoFirst();

    final SimpleCursorAdapter tempAdapter = new SimpleCursorAdapter(
        this,android.R.layout.simple_spinner_item,cur,new String[] { DbSchema.SiteSchema.COLUMN_LABEL },new int[] { android.R.id.text1 }
    );
    tempAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    siteSpn.setAdapter(tempAdapter);

    //fill the initial values
    String initSite = pref.getString(\"site_id\",\"New Site\");
    String spnLabel = null;
    final Cursor initCur = mDb.query(DbSchema.SiteSchema.TABLE_NAME,null,DbSchema.SiteSchema.COLUMN_LABEL + \"=?\",new String[] { initSite },null);
    initCur.movetoFirst();
    cur.movetoFirst();
    if(initCur.getCount()>0) {
        ipIn.setText(initCur.getString(initCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_IP)));
        portIn.setText(initCur.getString(initCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_PORT)));
        codeIn.setText(initCur.getString(initCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
        spnLabel = initCur.getString(initCur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
        labelIn.setText(spnLabel);
        if(cur.getCount()>0) {
            do {
                if(spnLabel.equals(cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL)))) {
                    siteSpn.setSelection(cur.getPosition());
                }
            } while(cur.movetoNext());
        }
    }

    siteSpn.setonItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
                cur.movetoFirst();
                if(cur.getCount()>0) {
                    do {
                        String tempLabel = cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
                        if(tempLabel.equals(((TextView)view.findViewById(android.R.id.text1)).getText().toString())) {
                            labelIn.setText(tempLabel);
                            portIn.setText(cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_PORT)));
                            ipIn.setText(cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_IP)));
                            codeIn.setText(cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_ACTIVATION_CODE)));
                        }
                    } while(cur.movetoNext());
                }
            }

            public void onnothingSelected(AdapterView<?> parent) {
                //
            }
        });

    deleteBtn.setonClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //do things
                cur.movetoFirst();
                while(cur.movetoNext()) {
                    String tempLabel = cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
                    View selectedSiteView = siteSpn.getSelectedView();
                    String label = ((TextView)selectedSiteView.findViewById(android.R.id.text1)).getText().toString();
                    if(tempLabel.equals(label)) {
                        mDb.delete(DbSchema.SiteSchema.TABLE_NAME,DbSchema.SiteSchema.COLUMN_LABEL+\"=?\",new String[] { label });
                        //dialogLayout.invalidate();
                        //dialog.dismiss();
                        //dialog.invalidate();
                        //v.getParent().invalidate();
                    }
                }
            }
        });

    builder.setPositiveButton(\"Save\",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                cur.movetoFirst();
                boolean newRecord = true;
                do {
                    String tempLabel = null;
                    if(cur.getCount()>0) {
                        tempLabel = cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
                    }
                    if(tempLabel!=null && tempLabel.equals(labelIn.getText().toString())) {
                        //update
                        ContentValues cv = new ContentValues();
                        cv.put(DbSchema.SiteSchema.COLUMN_IP,ipIn.getText().toString());
                        cv.put(DbSchema.SiteSchema.COLUMN_PORT,portIn.getText().toString());
                        cv.put(DbSchema.SiteSchema.COLUMN_ACTIVATION_CODE,codeIn.getText().toString());
                        MobileDashboardActivity.this.mDb.update(DbSchema.SiteSchema.TABLE_NAME,cv,new String[] { tempLabel });
                        newRecord = false;
                        break;
                    }
                } while(cur.movetoNext());
                if(newRecord) {
                    //new entry
                    ContentValues cv = new ContentValues();
                    cv.put(DbSchema.SiteSchema.COLUMN_IP,ipIn.getText().toString());
                    cv.put(DbSchema.SiteSchema.COLUMN_PORT,portIn.getText().toString());
                    cv.put(DbSchema.SiteSchema.COLUMN_LABEL,labelIn.getText().toString());
                    cv.put(DbSchema.SiteSchema.COLUMN_ACTIVATION_CODE,codeIn.getText().toString());
                    MobileDashboardActivity.this.mDb.insert(DbSchema.SiteSchema.TABLE_NAME,cv);
                }
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(\"site_id\",labelIn.getText().toString());
                editor.putString(\"activation\",codeIn.getText().toString());
                editor.commit();
                MobileDashboardActivity.this.writeCSVFile(\"dashboard_settings.csv\");
                dialog.dismiss();
            }
        });
    builder.setNegativeButton(\"Cancel\",int id) {
                dialog.cancel();
            }
        });
    dialog = builder.create();
    return dialog;
}
    

解决方法

我最终遵循以下示例: http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/ 它使用命令模式来触发外部逻辑: http://en.wikipedia.org/wiki/Command_pattern 这是文件: Command.java:
package com.conceptualsystems.dialog;

[...import statements...]

public interface Command {
    public void execute();

    public static final Command NO_OP = new Command() { public void execute() {} };
}
CommandWrapper.java:
package com.conceptualsystems.dialog;

[...import statements...]

public class CommandWrapper implements DialogInterface.OnClickListener {
    private Command command;
    public CommandWrapper(Command command) {
        this.command = command;
    }

    public void execute() {
        command.execute();
    }

    @Override
    public void onClick(DialogInterface dialog,int which) {
        dialog.dismiss();
        command.execute();
    }
}
MainActivity.java中的相关代码(成员变量定义):
Command delete = new Command() {
        public void execute() {
            removeDialog(DIALOG_WIFI_PREF);
            showDialog(DIALOG_WIFI_PREF);
        }
    };
使用Command类和CommandWrapper的对话框代码:
builder.setNeutralButton(\"Delete\",new CommandWrapper(delete) {
    public void onClick(DialogInterface dialog,int which) {
        //do things
        cur.moveToFirst();
        while(cur.moveToNext()) {
            String tempLabel = cur.getString(cur.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
            View selectedSiteView = siteSpn.getSelectedView();
            String label = ((TextView)selectedSiteView.findViewById(android.R.id.text1)).getText().toString();
            if(tempLabel.equals(label)) {
                mDb.delete(DbSchema.SiteSchema.TABLE_NAME,DbSchema.SiteSchema.COLUMN_LABEL+\"=?\",new String[] { label });
            }
        }
        dialog.dismiss();
        this.execute();
    }
});
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...