开发者

checked context menu items don't stay checked

I'm new to this forum and to android development itself so my question will probably be a very stupid one and i apologize for this . I began reading the Dev Guide on developer.android.com and until the part with the context menus everything worked pretty fine. Now i tried to have a context menu with a submenu that contains some checkable items. So i added the submenu and the items to my menu.xml and some item.setchecked(true) methods to my onContextItemSelected(...) method.

menu.xml

<?xml version="1.0" encoding="utf-8"?>  
  <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/info"
        android:title="@string/info" />
    <item android:title="@string/change">
        <menu>
           <item android:id="@+id/checkable_item1"
               android:checked="true"
               android:checkable="true"
               android:title="@string/hello"/>
           <item android:id="@+id/checkable_item2"
               android:checkable="true"
               android:title="@string/moin"/>
           <item android:id="@+id/checkable_item3"
               android:checkable="true" 
               android:title="@string/aloha"/>
       </menu>
  </item>   
</menu>

part of my .java file

...
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
      ContextMenuInfo menuInfo)
{
   super.onCreateContextMenu(menu, v, menuInfo);
   MenuInflater inflater1 = getMenuInfla开发者_如何学Cter();
   inflater1.inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item)
{
   switch(item.getItemId())
   {
   case R.id.checkable_item1:
      if(item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      return true;
   case R.id.checkable_item2:
      if(item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      return true;
   case R.id.checkable_item3:
      if(item.isChecked()) item.setChecked(false);
      else item.setChecked(true);
      return true;
   default:
      return super.onContextItemSelected(item);
   }
}
...

Now the problem is that when i open the menu and press one of the checkable items and i can see that the green tick pops up in the little box just before the context menu closes but when i open up the menu again the tick is gone. Now i don't really know why the tick doesn't stay in the box. It would be nice if someone could give me hint and tell me what i'm doing wrong. thanking you in anticipation

jean-claude91


I haven't tried it myself but if I read the description here properly (http://developer.android.com/reference/android/app/Activity.html#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)), your layout resource will be called every time the menu is created. Since it's "not safe to hold on to a menu after the method returns" , you would need to process the selected item and persist that selection somewhere and then pass the current state of the selectable items into the onCreate with menuInfo, setting checked/unchecked using that info.

If you don't, then the menu will be recreated every time based on your default settings (menu.xml).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜