ListActivity.getSelectedItemId()取不到值的问题

    今天研究动态菜单(sdk中的记事本例子google example:notepad)时发现 getSelectItemId() 无论如何拿到的都是Long.MIN_VALUE即-9223372036854775808

ContractedBlock.gif ExpandedBlockStart.gif View Code
    @Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
final boolean haveItems = getListAdapter().getCount() > 0;

// If there are any notes in the list (which implies that one of
// them is selected), then we need to generate the actions that
// can be performed on the current selection. This will be a combination
// of our own specific actions along with any extensions that can be
// found.
if (haveItems) {
// This is the selected item.
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());

  

网上多方查证无果,很多人有同样问题,有解决方法但不知道问题原因。最后终于在android issue上找到解释 http://code.google.com/p/android/issues/detail?id=259&can=1&q=getSelectedItemId&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars

原文如下:

Hi,

Is this a click performed in touch mode or with the keys? If it's in touch mode, then

this is normal. The concept of focus or selection does not exist in touch mode.

Instead, you should rely on the value passed to you in onListItemClick().

大意是说触屏模式下该方法不可用,因为触屏模式下没有foucs或selection的概念,你必须在onListItenClick()回调中获得选中id。

想想确实如此,只有键盘手机才会有上下键来选择item,由于我的测试机是nexus s,无方向键,才会纠结这么久....- -0 。用模拟器debug了下,结果正确。好了,现在你应该明白notepad例子中Notelist那两个动态菜单是为键盘机准备的了吧。

转载于:https://www.cnblogs.com/muliangcong/archive/2011/08/26/2155204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值