我正在尝试在片段中执行上下文菜单,但它不起作用

时间:2017-03-30 16:41:29

标签: android android-fragments android-contextmenu

我正在尝试在片段中执行上下文菜单,但它不起作用,这是我的代码。

public class FragmentHome extends Fragment {

    private String DATABASE_NAME = "Alarm.sqlite";
    private SQLiteDatabase Database;

    ListView listView;
    ArrayList<alarm> list;
    AdapterAlarm adapterAlarm;

    Intent intent;
    AlarmManager alarmManager;
    PendingIntent pendingIntent;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_fragment_home, container, false);
        registerForContextMenu(view.findViewById(R.id.lv));
        return  view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView = (ListView)getView().findViewById(R.id.lv);
        //khoi tao arraylist va adapter
        list = new ArrayList<alarm>();
        adapterAlarm = new AdapterAlarm(list, getActivity());
        //set adapter len listview
        listView.setAdapter(adapterAlarm);
        //đăng ký context menu
        getData();
    }

    @Override
    //create contextmenu
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getActivity().getMenuInflater().inflate(R.menu.menu_listview_home, menu);
    }

    @Override
    //catch the event on list
    public boolean onContextItemSelected(MenuItem item) {
        //AdapterContextMenuInfo info = (AdapterContextMenuInfo)  item.getMenuInfo();
        switch(item.getItemId()) {
            case R.id.edit:
                // Do some stuff
                break;
            case R.id.delete:
                // Do some stuff
                break;
        }
        return super.onContextItemSelected(item);
    }
}

任何季度答案都值得

0 个答案:

没有答案