ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 01:55:41 GMT


Sponsored Links
 Hi all,

  I have a problem when using getSeletedItemId() for a ListActivity.
The code should do something like this:
1. User selects one row of the list.
2. User invokes the menu option "Delete row".
3. onOptionsItemSelected() handles that case and asks for
confirmation:

        case MENU_ID_DEL:
            // Delete row
            rowId = getSelectedItemId();
            if (rowId >= 0) {
                Log.i(TAG, "Deleting row: " + rowId);
                AlertDialog ad = new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(R.string.confirm_delete)
                    .setPositiveButton(R.string.yes, new
DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int 
whichButton) {
                                // User clicked Yes so delete the contexts.
                                deleteSelectedRow();
                            }
                        })
                    .setNegativeButton(R.string.no, new
DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int 
whichButton) {
                                // User clicked No so don't delete (do nothing).
                            }
                        })
                    .show();
            }
            return true;

  It is unexpected to me that when calling getSelectedItemId() from
deleteSelectedRow() it returns -9223372036854775808.
  However if I use this code instead it works fine:
        case MENU_ID_DEL:
            // Delete row
           deleteSelectedRow();
            return true;

  Any ideas of my mistake?

  BTW, isn't it inconsistent to have a getSelectedItemId() for the
ListActivity and getSelectedId() for the ExpandableListActivity?

  Thank you for your responses.

  Best regards,

   Jorge
--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Mark Murphy » Sun, 07 Sep 2008 02:07:54 GMT


 


<snip>


I'm not sure it's a "mistake" per se, but a side-effect of dialogs. As 
it turns out, I just ran into this issue an hour or two ago, so it's 
fresh in my mind.

When you open the AlertDialog, the row in the ListActivity is no longer 
selected. I'd argue this is a bug -- opening a dialog should not affect 
the state of the underlying activity -- but there might be a good reason 
the Android team did it this way.

What you need to do is save getSelectedItemId() before opening the 
AlertDialog (e.g., final long selection=getSelectedItemId()), so you 
have the proper value for use in the dialog's positive button callback.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.1 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


ListActivity getSelectedItemId() and AlertDialog()

by Romain Guy » Sun, 07 Sep 2008 02:55:04 GMT


 > When you open the AlertDialog, the row in the ListActivity is no longer

It is not a bug. There is only one selection/focused widget on screen
at a time. Opening a dialog makes the underlying window loses the
focus, and when a window doesn't have focus, it does not contain any
selection or focused widget.

-- 
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Mark Murphy » Sun, 07 Sep 2008 03:33:56 GMT


 


The problem, though, isn't so much that "it does not contain any 
selection", is that you positively get rid of the selection, thereby 
changing the state.

For example:

-- Have a ListActivity with an option menu choice that displays a dialog
-- User selects a row in the list
-- User opens and closes the option menu -- the row is still selected
-- User opens the option menu and chooses the option that displays a 
dialog, then cancels out of the dialog, with no code that modifies the 
list contents -- the row selection is gone

By doing this, you're delegating the Principle of Least Surprise to the 
developer, since the user would not expect, for a no-change set of 
actions, to have to re-select the row. And the developer's own Principle 
of Least Surprise has failed, as the activity loses just as much focus 
when the option menu is displayed as when the dialog is displayed, yet 
the results differ -- in one case, UI state remains intact; in another 
case, UI state is altered.

Moreover, onSaveInstanceState() is not called before a dialog is popped 
over the activity, meaning we have to roll our own state-saving logic to 
deal with any state the act of popping a dialog will reset.

Are you *sure* this is the correct behavior?

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Warescription: All titles, revisions, & ebook formats, just $35/year

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 03:37:16 GMT


 Thanks to both.  That was my understanding, but then I run into a
problem with the java language. If I declare the rowId variable as
local in the onOptionsItemSelected() then the compiler complains that:
"local variable rowId is accessed from within inner class; needs to be
declared final".
And declaring it as a class variable seems like an overkill to me.

Best regards,

  Jorge



--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 03:43:50 GMT


 Adding on mark comments, I should say that the behaviour is not the
same with an ExpandableListActivity using getSelectedId().

Best regards,

  Jorge




--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Mark Murphy » Sun, 07 Sep 2008 03:45:20 GMT


 


As the error message says, declare it final:

final long rowId=getSelectedItemId();

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Warescription: All titles, revisions, & ebook formats, just $35/year

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Romain Guy » Sun, 07 Sep 2008 03:45:55 GMT


 It is the correct behavior. A ListView selection is here only for
**navigation** it is not meant as a persistent data selection tool.
For this, you have to use ListView's choice mode. Think of ListView's
selection as just a slightly different form of focus: it's there to
allow phones with only a dpad/trackball to navigate through the list
and click an element. Context-based actions are performed in Android
using the contextual menus (or long click/long press.) No contextual
action should be placed in the main menu (the one that appears with
the menu key.)

If the user selects a list row, then somehow pops up a dialog, then
dismiss the dialog, the list row will still be selected *unless* the
user entered touch mode. This will definitely get rid of the ListView
selection.

So again, do *not* use ListView's selection as a persistent state: it
is not, it's just a navigation tool for the user. Nothing more.







-- 
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Peli » Sun, 07 Sep 2008 03:46:05 GMT


 > The code should do something like this:

A remark:
As far as I understand, the proper Android way of doing this would be
to put the "Delete" command into the long-click context menu...
(either long-click by d-pad, or by touch). Context commands should not
appear in the options menu - only global commands (e.g. "Delete all")
should be placed there.
See onCreateContextMenu(...)

Peli
www.openintents.org

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 03:52:40 GMT


 Fair enough! Thank you.

  Jorge





--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 03:56:21 GMT


 Interesting. I didn't know this was the expeced behaviour. Could you
please point me to where this is documented? Thank you.

Best regards,

  Jorge



--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Jorge D Ortiz » Sun, 07 Sep 2008 03:56:28 GMT


 Interesting. I didn't know this was the expeced behaviour. Could you
please point me to where this is documented? Thank you.

Best regards,

  Jorge



--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Mark Murphy » Sun, 07 Sep 2008 03:59:38 GMT


 


OK.


FWIW, I don't think users will expect that behavior.

Thanks for the info!

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
Warescription: All titles, revisions, & ebook formats, just $35/year

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Romain Guy » Sun, 07 Sep 2008 04:01:55 GMT


 >> If the user selects a list row, then somehow pops up a dialog, then

It's not very surprising actually because every time you touch the
screen, you see focus/selection disappear. We've been extensively
using and testing Android on real devices at Google and so far no user
has complained about this. So I disagree :)

-- 
Romain Guy
www.curious-creature.org

--~--~---------~--~----~------------~-------~--~----~



ListActivity getSelectedItemId() and AlertDialog()

by Peli » Sun, 07 Sep 2008 04:02:58 GMT


 > Could you

Romain Guy is the living documentation :-) He posted something similar
above, when I posted it here.

Peli


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Speak Now Dialog

This is a head scratcher. I've been trying to Style the Speak Now
dialog. Any solutions?

-- 

2. cube rotating unexpectedly !!

Hi everyone ,im new to this forum ;)
hey buddies i am facing diffculty in solving some issues related to
[b]cube transformation[/b]s :cry:

Im trying to rotate a 3D cube on touch. the distance moved to left or
right is taken as the angle of rotation with respect to Y axis and
the  distance moved to up or down is taken as the angle of rotation
with respect to X axis .
 So these are the rotations im making
glrotate(angleXdistance, 0, 1, 0); // with respect to Y axis
glrotate(angleYdistance, 1, 0, 0); // with respect to X axis


so every time i add the angles with previous angles  lik
angleXdiatance + = angleXdiatance; in touch event

so the problem im facing is :
1.) when i rotate to right or left for 90 degree it rotates
corectly ,and after when i rotate to up or down it rotates with
respect to Z axis instead of Y axis
2.)when i rotate to right or left for 180 degree it rotates
corectly ,and after when i rotate to up it  rotates
down , to say in it rotates in opposite direction  .

i could analyse that after 90/180 degree roataions the default axes
changes , so it will roatate with respect to current axes , so i tried
to chk the condition if it crosses 90 thn instead of rotating with
respect to X rotate with respect to Z , and after 180 , reverse the
rotation angle sign .. it works   :D but nt always  :evil:  ..  sm
prblm occurs in sm other condition,,,

so i found out all posibilities , at certain angle of rotation , these
are the current axes  and thn passes those axes but still din work
coz  two rotations are happening so tht the number of possiblities
increaes which is nt possible to find all conditions..

so if anyone has idea how to solve this issue plz help ...  my cube
and my head rotating unexpectedly  :shock: plz  help  to rotate them
correctly  :roll:
i could see many examples or applications with such problem  there
also ...

-- 

3. I can't left/right scroll on listview inside a galleryview

4. ddms won't load anymore

5. issue with android browser

6. SimpleCursorAdapter and database update

7. bluetooth voice command will not work with smart?phones/Droids!!