2011-05-19 12 views
0

我在對話框佈局中放置了一個按鈕,我想要觸發對話框的完全重繪。但是,看起來你不能在onCreateDialog中打開對話框,並且我找不到一個不使用setPositiveButton之類的對話框的方法,因爲它是唯一的按鈕覆蓋,它將對話框作爲參數(我可以' t找到一種方法來獲取它是佈局的一部分的Button)。這裏的所有相關代碼:android:調用onCreateDialog中對話框的重繪

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, null, 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, DbSchema.SiteSchema.COLUMN_LABEL+"=?", 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, null, 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", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       dialog.cancel(); 
      } 
     }); 
    dialog = builder.create(); 
    return dialog; 
} 

回答

0

我結束了下面這個例子:

http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/

它使用Command模式來PROC外邏輯:

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