Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onContextItemSelected is not called in Fragment

I'm trying use the popup menu in long press in ListView. I can call the popup menu, but when i click in the option of this popup menu the onContextItemSelected() not works.

Here is my code:

package com.pa.homeautomationblank.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.pa.homeautomationblank.R;
import com.pa.homeautomationblank.listeners.OnItemCentraisLongClickListener;
import com.pa.homeautomationblank.listeners.OnReconhecerCentraisClickListener;
import com.pa.homeautomationblank.model.Central;
import com.pa.homeautomationblank.model.dao.CentralDAO;

public class CentraisFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_centrais, container,false);

        RelativeLayout rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_bottom);
        Button btnReconhecerCentrais = (Button) rLay.findViewById(R.id.btnReconhecerCentrais);

        btnReconhecerCentrais.setOnClickListener(new OnReconhecerCentraisClickListener());

        CentralDAO cDAO = new CentralDAO(view.getContext());
        ArrayAdapter<Central> adapter = new ArrayAdapter<Central>(view.getContext(), android.R.layout.simple_list_item_1 , cDAO.selectAll());

        rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_conteudo);
        ListView lv = (ListView) rLay.findViewById(R.id.lvCentrais);
        lv.setAdapter(adapter);
        lv.setOnItemLongClickListener(new OnItemCentraisLongClickListener());
        registerForContextMenu(lv);

        return (view);
    }

    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId()==R.id.lvCentrais){
            menu.setHeaderTitle(R.string.operacoes);
            String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
            for (int i=0;i<menuitems.length;i++){
                menu.add(Menu.NONE,i,i,menuitems[i]);
            }
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){

        int menuindex = item.getItemId();

        switch (menuindex) {
        case 0:
                Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
            break;

        default:
            Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
            break;
        }


        return super.onContextItemSelected(item);
    }

}

I put breakpoint in the method but the event is not called.

Thanks in advance!

like image 328
pedro.olimpio Avatar asked Dec 15 '22 15:12

pedro.olimpio


2 Answers

For anybody still looking for a workaround, Once I solved this issue by creating an anonymous OnMenuItemClickListener that delegates back to onContextItemSelected(MenuItem item) and setting it on all the items in my menu.

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
    // Creation/inflate menu here
    
    OnMenuItemClickListener listener = new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            onContextItemSelected(item);
            return true;
        }
    };
    
    for (int i = 0, n = menu.size(); i < n; i++)
        menu.getItem(i).setOnMenuItemClickListener(listener);
}
like image 140
Setu Kumar Basak Avatar answered Dec 21 '22 10:12

Setu Kumar Basak


I solved my problem.

I change the implementation of this method: onContextItemSelected to setOnMenuItemClickListener inside of onCreateContextMenu method as the following code:

@Override   
    public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        if (v.getId()==R.id.lvCentrais){
            menu.setHeaderTitle(R.string.operacoes);
            String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
            for (int i=0;i<menuitems.length;i++){
                menu.add(Menu.NONE,i,i,menuitems[i]);
            }
            for (int i=0;i<menu.size();i++){
                menu.getItem(i).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int menuindex = item.getItemId();

                        switch (menuindex) {
                        case 0:
                                Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
                            break;

                        default:
                            Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
                            break;
                        }
                        return false;
                    }
                });
            }
        }
    }

Thanks for all help!

like image 44
pedro.olimpio Avatar answered Dec 21 '22 10:12

pedro.olimpio