2011-04-28 68 views
3

嗨我有一个alertdialog,当你点击一个列表视图中的项目时创建,我试图从我的活动中获取文件,描述,作者等名称,并显示它在我的alertdialog中,但.setText不起作用,有人可以帮忙。谢谢,这里是我的代码:http://pastebin.com/FzWSPp5eAndroid AlertDialog SetText

回答

1

这完全不是你如何正确使用Android中的对话框。你需要在onCreateDialog的覆盖来定义对话框,如文档中描述:

http://developer.android.com/guide/topics/ui/dialogs.html 

这个指南,你应该能够解决您的问题。这是我刚才复制并从一个随机的应用程序粘贴的一个例子:

@Override 
protected Dialog onCreateDialog(int id, Bundle b) { 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE); 
    AlertDialog.Builder builder = null; 
    switch(id) { 
     case DIALOG_BLOCK_SIZE: 
     { 
      Dialog dialog = new Dialog(this); 
      final View dialogLayout = inflater.inflate(R.layout.dialog_block_size, null); 
      builder = new AlertDialog.Builder(this); 
      builder.setView(dialogLayout); 
      builder.setTitle("Set Block Size"); 

      final EditText blockIn = (EditText)dialogLayout.findViewById(R.id.block_size_in); 
      blockIn.setText(new Integer(pref.getInt("block_size", 6)).toString()); 

      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         SharedPreferences.Editor editor = pref.edit(); 
         editor.putInt("block_size", new Integer(blockIn.getText().toString())); 
         editor.commit(); 
         ////////TODO/////////// 
         //notify MinutemaidService that we have changed the block_size 
         dialog.dismiss(); 
        } 
       }); 
      builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 
        } 
       }); 
      dialog = builder.create(); 
      return dialog; 
     } 
     default: 
     { 
      return null; 
     } 
    } 
    return dialog; 
} 

与上面的代码,你可以调用的ShowDialog(DIALOG_BLOCK_SIZE),以显示该对话框。还要注意对话框会被创建一次并一遍又一遍地显示。强制重建对话框,在调用showDialog(int)之前调用removeDialog(int)。覆盖onPrepareDialog()是最好的方法,但使用removeDialog的工作,它更容易。