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. TextView & Movement Method Issue

hi,

when TextView's movement method is LinkMovementMethod i'm observing
weird behavior.

i have TextView inside ScrollView. TextView has some links
(ClickableSpans) and its text is quite large so it doesn't fit the
TextView's area. when i use DPAD down/up arrows TextView "jumps" on
links, scrolling accordingly to show current link. this is ok.

but when i for example jump to bottom link, then using touch scroll to
the middle of TextView and then try to use up DPAD arrow i have to
press up key several times to see any change in scrolling: as if up
arrow key "jumped" on links but without any visual effect (i mean
without any scrolling)

is it expected behavior or some kind of bug?

btw the same can be observed in Linkify demo when you enlarge text
size to some big value or add some extra text so that it doesn't fit
the screen.

thanks
pskink
--~--~---------~--~----~------------~-------~--~----~

2. advice sought - grid of thumbnails that do something when you click on them.

i want to create a scrollable grid of thumbnail images with text
underneath each thumbnail.  when you click on one it will do
something.

should i use a GridView, a multi-column ListView, a TableLayout, or
other??

thoughts?

tia.
--~--~---------~--~----~------------~-------~--~----~

3. How to capture and save the image of Google Map(MapView)...

4. Triggering options list

5. Android Animation. How to remove flashing effect ?

6. Monitoring cpu charge/usage : is it possible?

7. Receiving text from a remote source and pasting into the current application