views:

58

answers:

1

Hi. I'm using and ArrayAdapter to populate a ListView. After selecting and item, it displays a confirmation Y/N dialog. If the user's choice is negative, then he should be able to select another item showing the same dialog. And so on.

Here's my code:

lView.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {


       Toast.makeText("Selected file"+ mFiles.get(index).getfileName(),
       Toast.LENGTH_SHORT).show();


       SelectedFile = mFiles.get(index);



          showDialog(DIALOG_CONFIRMIMPORT_ID);
       }
    });

The weird thing is that while the "Toast" shows the clicked item every time, only the first selected item since the Activity is initiated is being passed to "SelectedFile". No matter how many times you click a diferent item, "SelectedFile" always assumes the same value, the value of the first clicked item, outside of this code.

Heres's my Dialog code:

Protected Dialog onCreateDialog(int id) {
switch(id) {
 case DIALOG_CONFIRMIMPORT_ID:
  {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  String message = String.format(getString(R.string.importstudentfileconfirm),SelectedFile.getfileName());

  builder.setMessage(message)
  .setCancelable(false)
  .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
  //     Activity.this.finish();
  //     startActivity(new Intent(Activity.this, LOL.class));
  }
  })
 .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int id) {
 SelectedFile = null;
 dismissDialog(DIALOG_CONFIRMIMPORT_ID);
 mFiles.notifyAll();
 }
 });

 AlertDialog alert = builder.create();
 return alert;
 }
 }
  return null;
 }

Thank you very much for any help!

A: 

I'm guessing this has something to do with the fact that the onCreateDialog method is only called the first time the dialog is created. So the first time you see the dialog it will have the correct filename.

After onCreateDialog is called, onPrepareDialog(...) is called. onPrepareDialog, allows you to change the dialog after it has been created, but before it gets displayed.

Remember that underneath everything, Android isn't creating a new Dialog for you every time you want to show the DIALOG_CONFIRMIMPORT_ID dialog. It is too computationally expensive to instantiate a new dialog every time. Instead, it creates it once, which causes onCreatDialog to be called, followed by the onPrepareDialog. Every other time the dialog is shown, it only calls onPrepareDialog.

Check out the following article on the Android Developer site. It explains things pretty clearly.

http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

So try using onCreateDialog just for initialization of stuff that won't change between showings of the dialog, then use the onPrepareDialog method to dynamically update the contents of the dialog (i.e. getting the new filename)

Cheers!

James
You're completely right! Thank you very much for your quick answer. Cheers!
Marco Sousa
If this answered your question, could you accept the answer by clicking on the check box outline to the left of the answer. Thanks!
James
Done. For some reason the checkbox isn't showing on my Firefox.
Marco Sousa
Muchas Gracias! :)
James