如何将变量或对象传递给Android中的对话框 [英] How to pass a variable or object to dialog in android

查看:125
本文介绍了如何将变量或对象传递给Android中的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a custom dialog with one editText view and two buttons ok and cancel. I have a custom list view displaying some rows of data fetched from database. When the user clicks on the row of the list view, custom dialog box is shown to the user to edit the selected row. What i want to do is to be able to pass the object binded with the selected row to the dialog box so that i could display the data being edited.

这是我的活动课:

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

推荐答案

使用showDialog(CUSTOM_DIALOG_ID)代替使用showDialog(CUSTOM_DIALOG_ID),您可以创建带有参数的自己的方法,并且可以使用AlertDialog显示包含textview和按钮的视图

Instead of using showDialog(CUSTOM_DIALOG_ID) use you can create a your own method with argument and in that you can use AlertDialog to display your view that contains textview and buttons.

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

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

ii) Instead of showDialog(CUSTOM_DIALOG_ID) use createDialog(cmt)

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

iV) also instead of dismissDialog(CUSTOM_DIALOG_ID) use alert.dismiss();

这篇关于如何将变量或对象传递给Android中的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆