2011-11-21 44 views
0

我的警報對話框有問題,我有一個自定義對話框,其上有3個edittext結尾3個textview。當我從聯繫人中選取一個聯繫人時,我只是填充了dilaog edtitext中的信息,如contactName.setText(bla);然而,在這裏發生的奇怪事情是,如果我取消對話框並再次選擇另一個聯繫人,對話框中的細節沒有變化並記住第一次選擇的聯繫人的詳細信息?是不是很奇怪?它似乎一旦它創建對話框,即使我再次調用相同的進程創建對話框,它仍然保持第一個對話框並繼續顯示相同的對話框。有沒有人有過相同的經歷並知道如何解決這個問題?Android AlertDialog會記住之前顯示的字符串嗎?

下面是處理從聯繫人選擇器返回結果的代碼。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (resultCode == RESULT_OK) { 
       switch (requestCode) { 
       case CONTACT_PICKER_RESULT: 
        Cursor cursor = null; 
        String email = ""; 
        try { 
         Uri result = data.getData(); 
         Log.v(DEBUG_TAG, "Got a contact result: " 
           + result.toString()); 

         // get the contact id from the Uri 
         String id = result.getLastPathSegment(); 

         // query for everything email 
         cursor = getContentResolver().query(Email.CONTENT_URI, 
           null, Email.CONTACT_ID + "=?", new String[] { id }, 
           null); 

         int emailIdx = cursor.getColumnIndex(Email.DATA); 

         // let's just get the first email 
         if (cursor.moveToFirst()) { 

          email = cursor.getString(emailIdx); 
          contactUEmail=email; 
          Log.v(DEBUG_TAG, "Got email: " + email); 
         } else { 
          Log.w(DEBUG_TAG, "No results"); 
         } 
        } catch (Exception e) { 
         Log.e(DEBUG_TAG, "Failed to get email data", e); 
        } finally { 
         if (cursor != null) { 
          cursor.close(); 
         } 
         // EditText emailEntry = (EditText) findViewById(R.id.invite_email); 
         // emailEntry.setText(email); 
         if (email.length() == 0) { 
          Toast.makeText(this, "No email found for contact.", 
            Toast.LENGTH_LONG).show(); 
         } 

        } 

        showDialog(DIALOG_ADD_NEW_CALL);// this is where I call the dialog. 
        break; 
       } 

      } else { 
       Log.w(DEBUG_TAG, "Warning: activity result not ok"); 
      } 
     } 

這裏是對話框DIALOG_ADD_NEW_CALL;

case DIALOG_ADD_NEW_CALL: 
     { 
     builder = new AlertDialog.Builder(this); 
     entryView = getLayoutInflater().inflate(R.layout.entry, null); 
     builder.setView(entryView); 
     CNameEditor = (EditText) entryView.findViewById(R.id.cName); 
     CEmailEditor = (EditText) entryView.findViewById(R.id.cEmail); 
     CPhoneEditor = (EditText) entryView.findViewById(R.id.cPhone); 

     if(!contactUEmail.equals(""))//here is the code that I am setting the text. 
     CEmailEditor.setText(contactUEmail); 
     else{} 

     builder.setTitle(R.string.addDialogTitle); 
     builder.setPositiveButton(R.string.addItem, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 

     builder.setNegativeButton(R.string.cancelItem, 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         dialog.cancel(); 

        } 
       }).create(); 

     return builder.create(); 

     } 
+0

一些代碼將是要命。這可能是任何事情。聽起來像你只需設置一次值。有沒有機會檢查設置的值是否不再設置它們或類似的東西?或者,如果視圖已經被誇大了,不要再重複一次?請提供更多內容:) – Tobias

+0

除非您顯示您使用的代碼,否則任何人都難以給出答案。 – Squonk

+0

我剛剛編輯並將代碼放在那裏。我希望你能看到我犯的錯誤:)這真的很煩人。我花了一半的時間來解決這個問題,仍然一樣:) – akd

回答

0

覆蓋onPrepareDialog

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
    switch (id) { 
    case DIALOG_ADD_NEW_CALL: 
     AlertDialog callDialog = (AlertDialog) dialog; 
     View entryView = callDialog; 
     CNameEditor = (EditText) entryView.findViewById(R.id.cName); 
     CEmailEditor = (EditText) entryView.findViewById(R.id.cEmail); 
     CPhoneEditor = (EditText) entryView.findViewById(R.id.cPhone); 
     // do stuff to those variables here 
    } 
}