0

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.

Here is my activity class:

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;
}
}
rockstar
  • 1,322
  • 1
  • 20
  • 37

2 Answers2

1

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) 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) also instead of dismissDialog(CUSTOM_DIALOG_ID) use alert.dismiss();

Gunhan
  • 6,807
  • 3
  • 43
  • 37
Ishu
  • 5,357
  • 4
  • 16
  • 17
  • could you please give me an example of how to do so – rockstar Mar 31 '12 at 14:40
  • i tried this but in the line dialog_editComment = (TextView)dialog.findViewById(R.id.editComment); i get error message like method findViewById is undefined for the type AlertDialog.Builder – rockstar Mar 31 '12 at 15:13
  • please recheck now replaced dialog with view in few placess. – Ishu Mar 31 '12 at 15:17
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 .....
Ishu
  • 5,357
  • 4
  • 16
  • 17
  • doing so i can access it from onCreate method of dialog but the problem is i get the same value on dialog box – rockstar Mar 31 '12 at 14:41
  • I didnt get you can you be more specific on get the same value means – Ishu Mar 31 '12 at 14:46
  • clicking on any row on the listview, i get the same cmt object ie i m displaying the cmt.comment inside the dialog box, so clicking on any row i get same cmt.comment value – rockstar Mar 31 '12 at 14:59
  • yes i did the same, cmt.getComment() returns same value on every row i click – rockstar Mar 31 '12 at 15:12