2012-03-31 65 views
0

我有一个自定义对话框,其中一个editText视图和两个按钮ok并取消。我有一个自定义列表视图,显示从数据库中提取的一些数据行。当用户点击列表视图的行时,自定义对话框向用户显示,以编辑选定的行。我想要做的是能够将与选定行绑定的对象传递给对话框,以便我可以显示正在编辑的数据。如何将变量或对象传递给android中的对话框

这里是我的活动类:

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

回答

1

而不是使用的ShowDialog(CUSTOM_DIALOG_ID)使用,你可以创建一个带有参数自己的方法,并可以使用AlertDialog来显示你的观点,即包含的TextView和按钮。

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

ⅱ)代替的ShowDialog(CUSTOM_DIALOG_ID)使用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)也代替dismissDialog(CUSTOM_DIALOG_ID)使用alert.dismiss();

+0

请你给我怎么做 – rockstar 2012-03-31 14:40:33

+0

我想这一个例子,但在该行dialog_editComment =(TextView的)dialog.findViewById(R.id.editComment);我得到的错误消息像方法findViewById是未定义的类型AlertDialog.Builder – rockstar 2012-03-31 15:13:52

+0

请重新检查现在替换对话与视图在几个placers。 – Ishu 2012-03-31 15:17:08

0
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 ..... 
+0

这样做,我可以从onCreate方法的对话框访问它,但问题是我在对话框中获得相同的值 – rockstar 2012-03-31 14:41:40

+0

我没有得到你你可以更具体地获得相同的值意味着 – Ishu 2012-03-31 14:46:21

+0

点击列表视图上的任何一行,我就会得到相同的cmt对象,即在对话框中显示cmt.comment,所以点击任何一行我都会得到相同的cmt.comment值 – rockstar 2012-03-31 14:59:31

相关问题