0

I have an application in Android,

This application has a ListView with this event.

ListView titulos = (ListView)findViewById(R.id.lv_titulos);
    titulos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            position_item_selected = position;
            switch (position){
               case 0:
                    titulo = getString(R.string.Brands);
                    menu_contextual = rellenar_menu_contextual(brands);
                    break;
               case 1:
                    titulo = getString(R.string.Collections);
                    menu_contextual = rellenar_menu_contextual(collections);
                    break;
               case 2:
                   titulo = getString(R.string.References);
                   menu_contextual = rellenar_menu_contextual(references);
                   break;
               case 3:
                   titulo = getString(R.string.Colours);
                   menu_contextual = rellenar_menu_contextual(colours);
                   break;
               case 4:
                   titulo = getString(R.string.Sizes);
                   menu_contextual = rellenar_menu_contextual(sizes);
                   break;
           }
            registerForContextMenu(view);
            openContextMenu(view);
            unregisterForContextMenu(view);
        }
    });

This works fine if you click on item in listview, and click in item on contextmenu that it shows, but if u click on an item on list view, and then, when is showed context menu, if u click in back button, and return to view, u cant click again in item on list view.

If u click in other item, and click some item in context menu, all items works fine again.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(titulo);
    for (String aMenu_contextual : menu_contextual) menu.add(0, v.getId(), 0, aMenu_contextual);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    String[][] values;
    switch (position_item_selected){
        case 0:
            values = new String[][] {{getString(R.string.Brand),item.toString()}};
            brandSelec = item.toString();
            collections(brands.get(item.toString()).toString());
            break;
        (...)
        default:
            values = new String[][] {};
    }
    final StableArrayAdapter adapter = new StableArrayAdapter(SearchArticle.this,values);
    ListView titulos = (ListView)findViewById(R.id.lv_titulos);
    titulos.setAdapter(adapter);
    return true;
}

Why an item blocks in list view when u press back button in context menu?

Sorry for my bad english, Thank u.

Victor Company
  • 1,177
  • 1
  • 9
  • 9

1 Answers1

0

this code is the problematic part :

registerForContextMenu(view);
openContextMenu(view);
unregisterForContextMenu(view);

not only you register and then unregister, but you do it when clicking on an item in the list.

i don't understand what you tried to to here, but this is the correct way:

... onCreate() {
...
registerForContextMenu(listView);

this way, long clicking on an item would show the context menu, just like how it works on any other app.

android developer
  • 114,585
  • 152
  • 739
  • 1,270