【问题标题】:How to pass a variable or object to dialog in android如何将变量或对象传递给android中的对话框
【发布时间】:2012-04-14 22:14:15
【问题描述】:

我有一个自定义对话框,其中包含一个 editText 视图和两个按钮 ok 和 cancel。我有一个自定义列表视图,显示从数据库中获取的一些数据行。当用户单击列表视图的行时,将向用户显示自定义对话框以编辑所选行。我想要做的是能够将与所选行绑定的对象传递给对话框,以便我可以显示正在编辑的数据。

这是我的活动课:

public class TestDatabaseActivity extends ListActivity {
private CommentsDataSource datasource;
private CommentAdapter adt;

static final int CUSTOM_DIALOG_ID = 0;
private TextView dialog_editComment;
private EditText dialog_txtEditComment;
private Button dialog_btnOk, dialog_btnCancel;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    datasource = new CommentsDataSource(TestDatabaseActivity.this);
    datasource.open();
    getList();
}
private void getList()
{
    List<Comment> values = datasource.getAllComments();
    adt=new CommentAdapter(TestDatabaseActivity.this,R.layout.comment_row,values);
    setListAdapter(adt);    
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    CommentAdapter adapter= (CommentAdapter) getListAdapter();
    final Comment cmt = adapter.mListComment.get(position);
    System.out.println(cmt.getId()+cmt.getComment());

            //cmt is the object which i want to pass to my dialog
    showDialog(CUSTOM_DIALOG_ID);

}

   private Button.OnClickListener customDialog_UpdateOnClickListener = new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  //save the value and update list
 }

   };

   private Button.OnClickListener customDialog_DismissOnClickListener
   = new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  dismissDialog(CUSTOM_DIALOG_ID);
 }

   };

@Override
protected Dialog onCreateDialog(int id) {
 // TODO Auto-generated method stub
 Dialog dialog = null;;
    switch(id) {
    case CUSTOM_DIALOG_ID:
     dialog = new Dialog(TestDatabaseActivity.this);

     dialog.setContentView(R.layout.comment_edit_dialog);
     dialog.setTitle("Edit");

     dialog_editComment = (TextView)dialog.findViewById(R.id.editComment);
     dialog_txtEditComment = (EditText)dialog.findViewById(R.id.txtComment);
     dialog_btnOk = (Button)dialog.findViewById(R.id.btnOk);
     dialog_btnCancel = (Button)dialog.findViewById(R.id.btnCancel);

     dialog_btnOk.setOnClickListener(customDialog_UpdateOnClickListener);
     dialog_btnCancel.setOnClickListener(customDialog_DismissOnClickListener);
     break;
    }
    return dialog;
}
}

【问题讨论】:

    标签: android customdialog


    【解决方案1】:

    除了使用 showDialog(CUSTOM_DIALOG_ID) 之外,您还可以使用参数创建自己的方法,并且可以使用 AlertDialog 来显示包含文本视图和按钮的视图。

    i)   private AlertDialog alert;   should be declared in class scope above oncreate().
    

    ii) 使用 createDialog(cmt) 代替 showDialog(CUSTOM_DIALOG_ID)

    iii) private void createDialog(Comment cmt){
            AlertDialog.Builder dialog = new AlertDialog.Builder(TestDatabaseActivity.this);
            View view = _inflater.inflate(R.layout.comment_edit_dialog,null);
            dialog.setTitle("Edit");
    
            dialog_editComment = (TextView)view .findViewById(R.id.editComment);
            dialog_txtEditComment = (EditText)dialog.findViewById(R.id.txtComment);
            dialog_btnOk = (Button)view .findViewById(R.id.btnOk);
            dialog_btnCancel = (Button)view .findViewById(R.id.btnCancel);
    
            dialog_btnOk.setOnClickListener(customDialog_UpdateOnClickListener);
            dialog_btnCancel.setOnClickListener(customDialog_DismissOnClickListener);
            dialog.setView(view);
            //dialog.show();
            alert = dialog.create();
            alert.show();
        }
    

    iV) 也可以使用 alert.dismiss() 代替 dismissDialog(CUSTOM_DIALOG_ID);

    【讨论】:

    • 你能给我一个例子来说明如何做到这一点
    • 我试过了,但在 dialog_editComment = (TextView)dialog.findViewById(R.id.editComment);我收到错误消息,例如方法 findViewById 未定义类型 AlertDialog.Builder
    • 请在少数地方重新检查已替换的对话框。
    【解决方案2】:
    Also another solution to your problem is change the scope of cmt.  
    
    i.e., Above onCreate() declare 
    
    private Comment cmt; 
    
    now it can be access the TestDatabaseActivity. in your code make a minor change and try 
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        CommentAdapter adapter= (CommentAdapter) getListAdapter();
        cmt = adapter.mListComment.get(position);
        System.out.println(cmt.getId()+cmt.getComment());
    
                //cmt is the object which i want to pass to my dialog
        showDialog(CUSTOM_DIALOG_ID);
    }
    
    also declare private Comment cmt = null; above oncreate() and then in onCreateDialog() you can access 
    
    System.out.println(cmt.getId()+cmt.getComment());
    
    Try .....
    

    【讨论】:

    • 这样做我可以从对话框的 onCreate 方法访问它,但问题是我在对话框中得到相同的值
    • 我没明白你能不能更具体地了解获得相同的价值意味着
    • 单击列表视图上的任何行,我得到相同的 cmt 对象,即我在对话框中显示 cmt.comment,因此单击任何行我得到相同的 cmt.comment 值
    • 是的,我也这样做了,cmt.getComment() 在我点击的每一行上返回相同的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    相关资源
    最近更新 更多