1

I have a TabHost with 2 Fragments, both lists. The onCreateContextMenu works fine for both, but both onContextItemSelected are "pointing" to the first Fragment class. I'm using different names for the lists in the XML files, so the ID isn't the same.

Here is the code for both onCreateContextMenu

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        int itemID = info.position;

        Map m = (HashMap) this.listView.getAdapter().getItem(itemID);

        menu.setHeaderTitle(getString(R.string.options));
        menu.add(0, v.getId(), 0, activity.getString(R.string.delete));
    }

And here onContextItemSelected for the first Fragment, second one is pretty much the same code just changing var names. In fact this doesn't change anything to the question, since this method only runs on the first Fragment, even when I'm on the second list.

@Override
    public boolean onContextItemSelected(MenuItem item) {
        ContextMenuInfo menuInfo = (ContextMenuInfo) item.getMenuInfo();
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        int itemID = info.position;
        Map m = (HashMap) this.listView.getAdapter().getItem(itemID);
        processId = Integer.parseInt(m.get("processId").toString());

        activity.setSupportProgressBarIndeterminateVisibility(true);
        runBackground(activity.getString(R.string.accessingECM), false, false, ACTION_REMOVE);
        return true;
    }

This is how I register for context menu on the onActivityCreated method. Note that the onCreateContextMenu works for both Fragment.

listView = (ListView) this.view.findViewById(R.id.listProcess);
        registerForContextMenu(listView);

I tried to change the menu.add() 1st and 3rd parameters to (1,1) and (1,2). Also tried both to be Menu.NONE. Still doesn't work.

Thanks

4

1 に答える 1

0

タブホストの場合 (私の経験では)、タブホストのアクティビティで単一の onCreateContextMenu を設定し、スイッチを使用して適切なメニュー/アクションを取得する必要があります。

例:

// ***************************************************************
// Create the various context menus depending on which list it is from
// ***************************************************************
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    switch (v.getId()) {
    case R.id.list1: {
        menu.setHeaderTitle("Tool CC Menu");
        menu.add(0, v.getId(), 0, "Edit/Add Cutter Comp Info");
        menu.add(0, v.getId(), 0, "Clear Cutter Comp Info");
        break;
    }
    case R.id.list2: {
        menu.setHeaderTitle("WPC Menu");
        menu.add(0, v.getId(), 0, "Edit WPC Info");
        menu.add(0, v.getId(), 0, "Clear WPC Info");
        break;
    }
    }
}
// ***************************************************************
// Create the various context menu actions based on which list
// ***************************************************************
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    cc_id_int = (int) info.id;
    if (item.getTitle() == "Edit/Add Cutter Comp Info") {
        showDialog(CCEDIT_DIALOG_ID);
    } else if (item.getTitle() == "Clear Cutter Comp Info") {
        showDialog(CLEARCC_DIALOG_ID);
    } else if (item.getTitle() == "Edit WPC Info") {
        removeDialog(WPCEDIT_DIALOG_ID);
        showDialog(WPCEDIT_DIALOG_ID);
    } else if (item.getTitle() == "Clear WPC Info") {
        showDialog(CLEARWPC_DIALOG_ID);
    }
    return super.onContextItemSelected(item);
}

お役に立てれば!

于 2012-05-07T14:54:53.230 に答える