2012-03-31 8 views
0

私は1つのeditTextビューと2つのボタンがあるOKとキャンセルのカスタムダイアログを持っています。私はデータベースからフェッチされたデータのいくつかの行を表示するカスタムリストビューを持っています。ユーザーがリストビューの行をクリックすると、選択された行を編集するためのカスタムダイアログボックスがユーザーに表示されます。私がしたいのは、選択した行にバインドされたオブジェクトをダイアログボックスに渡して、編集中のデータを表示できるようにすることです。ここで変数やオブジェクトをアンドロイドでダイアログに渡す方法

は、私の活動のクラスです:

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)を使用してのあなたは、引数を使用して独自のメソッドを作成することができ、その中にあなたがのTextViewとボタンがあり、あなたのビューを表示するためにAlertDialogを使用することができます使用。

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

II)の代わりに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

+0

私はこれを試みたが、ラインdialog_editComment =(TextViewの)dialog.findViewById(R.id.editComment)で行う方法の例を与えてください可能性があり、メソッドのようなエラーメッセージが表示されるfindViewByIdはAlertDialog.Builderタイプのために定義されていません – rockstar

+0

今すぐ再確認してください。 – Ishu

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

+0

同じ値を取得するともっと具体的になりますか? – Ishu

+0

リストビューの任意の行をクリックすると、同じcmtオブジェクトが表示されます。つまり、cmt.commentがダイアログボックス内に表示されます。同じ行をクリックすると、同じcmt.comment値が得られます。 – rockstar

関連する問題