如何在android中将变量或对象传递给对话框
我有一个自定义对话框,其中有一个editText视图和两个按钮ok和cancel。我有一个自定义列表视图,显示从数据库获取的一些数据行。当用户单击列表视图的行时,将向用户显示“自定义”对话框以编辑所选行。我想做的是能够将与所选行绑定的对象传递到对话框,以便显示正在编辑的数据 这是我的活动课:如何在android中将变量或对象传递给对话框,android,customdialog,Android,Customdialog,我有一个自定义对话框,其中有一个editText视图和两个按钮ok和cancel。我有一个自定义列表视图,显示从数据库获取的一些数据行。当用户单击列表视图的行时,将向用户显示“自定义”对话框以编辑所选行。我想做的是能够将与所选行绑定的对象传递到对话框,以便显示正在编辑的数据 这是我的活动课: public class TestDatabaseActivity extends ListActivity { private CommentsDataSource datasource; private
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;
}
}
公共类TestDatabaseActivity扩展了ListActivity{
私有评论数据源;
专用适配器adt;
静态最终整型自定义对话框ID=0;
私有文本视图对话框_editComment;
私有编辑文本对话框\u txtEditComment;
私有按钮对话框\u btnOk,对话框\u btncel;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datasource=newcommentsdatasource(TestDatabaseActivity.this);
datasource.open();
getList();
}
私有void getList()
{
列表值=datasource.getAllComments();
adt=新的CommentAdapter(TestDatabaseActivity.this,R.layout.comment\u行,值);
setListAdapter(adt);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
super.onListItemClick(左、右、位置、id);
CommentAdapter=(CommentAdapter)getListAdapter();
最终注释cmt=adapter.mListComment.get(位置);
System.out.println(cmt.getId()+cmt.getComment());
//cmt是我要传递给对话框的对象
showDialog(自定义对话框ID);
}
private Button.OnClickListener自定义对话框\u UpdateOnClickListener=新建按钮。OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
//保存值并更新列表
}
};
private Button.OnClickListener自定义对话框\u DismissOnClickListener
=新建按钮。OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
dismissDialog(自定义对话框ID);
}
};
@凌驾
受保护的对话框onCreateDialog(int id){
//TODO自动生成的方法存根
Dialog=null;;
开关(id){
案例自定义对话框\u ID:
dialog=新建对话框(TestDatabaseActivity.this);
setContentView(R.layout.comment\u edit\u对话框);
对话框。设置标题(“编辑”);
dialog_editComment=(TextView)dialog.findViewById(R.id.editComment);
dialog_txtEditComment=(EditText)dialog.findViewById(R.id.txtComment);
dialog_btnOk=(按钮)dialog.findviewbyd(R.id.btnOk);
dialog_btnCancel=(按钮)dialog.findviewbyd(R.id.btnCancel);
dialog\u btnOk.setOnClickListener(customDialog\u UpdateOnClickListener);
dialog\u btnCancel.setOnClickListener(customDialog\u DismissOnClickListener);
打破
}
返回对话框;
}
}
不用showDialog(自定义对话框ID)使用,您可以使用参数创建自己的方法,并且可以使用AlertDialog显示包含textview和按钮的视图
i) private AlertDialog alert; should be declared in class scope above oncreate().
ii)使用createDialog(cmt)代替showDialog(自定义对话框ID)
iV)也可以使用alert.dismise()代替dismissloal(自定义对话框ID) 你能给我举一个如何做soi的例子吗?我试过了,但是在行对话框中,_editComment=(TextView)dialog.findViewById(R.id.editComment);我收到错误消息,如类型AlertDialog的方法findViewById未定义。Builderplease recheck现在已在少数位置用视图替换了dialog。这样做,我可以从dialog的onCreate方法访问它,但问题是我在dialog Box上获得相同的值我没有得到你可以更具体地获得相同的值意味着单击对话框上的任何行吗在listview中,我得到了相同的cmt对象,即我在对话框中显示了cmt.comment,因此单击任何一行我都会得到相同的cmt.comment值是的,我做了相同的操作,cmt.getComment()会在我单击的每一行上返回相同的值
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();
}
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 .....