onContextMenuClosed за клас, разширяващ DialogFragment

Имам клас, разширяващ DialogFragment и в това показвам ListView. Искам да имам опция LongPress за елементите в този списък. Така че отмених onCreateContextMenu и onContextItemSelected. Показва ми се контекстното меню с правилните опции.

Проблемът, който имам, е, че не мога да извикам getDialog().dismiss() от метода onContextItemSelected.

Какъв е правилният начин за затваряне на клас, разширяващ DialogFragment, от метода onContextItemSelected на класа, разширяващ DialogFragment?

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    if(view.getId() == listView.getId())
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        menu.setHeaderTitle(industry.name);
        menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
    }
}

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onContextItemSelected(android.view.MenuItem)
 */
@Override
public boolean onContextItemSelected(MenuItem item)
{
    if(item.getItemId() == USE_INDUSTRY)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
        getDialog().dismiss();
    }

    return true;
}

person Cassie    schedule 04.12.2012    source източник
comment
Добавих кода, както беше поискано   -  person Cassie    schedule 04.12.2012


Отговори (1)


Намерих отговора, onContextItemSelected() не беше извикан в моя диалогов фрагмент. Което означаваше, че getDialog().dismiss() не се обаждаше.

Единственият начин, по който мога да намеря това, е да задам onMenuItemClickListener за MenuItem и да регистрирам щракването там. По-долу е последният код, който използвам.

/*
 * (non-Javadoc)
 * @see android.support.v4.app.Fragment#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
    if(view.getId() == listView.getId() && isIndustryLevel)
    {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        Industry industry = (Industry) listView.getItemAtPosition(info.position);

        menu.setHeaderTitle(industry.name);
        menu.add(Menu.NONE, USE_INDUSTRY, 0, USE_INDUSTRY_TEXT);
        menu.getItem(0).setOnMenuItemClickListener(new OnUseIndustryButtonClick());
    }
}

private class OnUseIndustryButtonClick implements MenuItem.OnMenuItemClickListener
{
    /*
     * (non-Javadoc)
     * @see android.view.MenuItem.OnMenuItemClickListener#onMenuItemClick(android.view.MenuItem)
     */
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        Industry industry = app.industries.get(info.position);

        MyApplication.BUS.post(new IndustryEvent(industry.ID, -2));
        getDialog().dismiss();
        return true;
    }       
}
person Cassie    schedule 05.12.2012