【问题标题】:Troubles with ContextMenu in ExpandableListActivityExpandableListActivity 中的 ContextMenu 问题
【发布时间】:2011-04-30 11:47:05
【问题描述】:

ExpandableListActivity 中,我注册了ContextMenu。我要做的是存储按下ContextMenu 的组的子列表项的数据。 根据:

onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo)

v 是为其构建上下文菜单的视图。所以这个视图应该是我单击的列表项的视图,但它不是,它指的是子列表中的第一个列表项。我相信它应该返回为其构建上下文菜单的列表项的视图,但这里不是这种情况。这是我的代码:

public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("My Crumbs");

        TextView rowid = (TextView) v
                .findViewById(R.id.trackdetails_item_row_id);
        rowId = rowid.getText().toString();

        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
        int type = ExpandableListView
                .getPackedPositionType(info.packedPosition);

        // Only create a context menu for the child
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {

            TextView trackstats = (TextView) v
                    .findViewById(R.id.trackdetails_item_stats);
    menu.add(0, MENU_SHARE, 0, "Share on Facebook");
        }

    }

有人可以解释一下吗?

编辑:

ExpandableListAdapter 的代码:

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {

        public MyExpandableListAdapter(Cursor cursor, Context context,
                int groupLayout, int childLayout, String[] groupFrom,
                int[] groupTo, String[] childrenFrom, int[] childrenTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo,
                    childLayout, childrenFrom, childrenTo);
            setViewBinder(viewBinder);
        }

        @Override
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            // TODO Auto-generated method stub
            String crumbName = groupCursor.getString(mCrumbNameColumnIndex);
            return crumpareDBAdapter.getTrackList(mTracksProjection, crumbName);
        }

        @Override
        public SimpleCursorTreeAdapter.ViewBinder getViewBinder() {
            return viewBinder;
        }

    }

ViewBinder的代码:

SimpleCursorTreeAdapter.ViewBinder viewBinder = new ViewBinder() {

        @Override
        public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
            // TODO Auto-generated method stub
         TextView textView = (TextView) view;
         textView.setText(cursor.getString(columnIndex));
            return true;
        }
    };

【问题讨论】:

  • 您可能无法在适配器中回收列表项。你也可以分享那个代码吗?谢谢!
  • 这是我正在使用的适配器的代码。感谢您的帮助
  • 您在viewBinder 中定义了什么?您可以使用BaseExpandableListAdapter 实现,它会更清晰
  • 我在这里做类似的事情!!!stackoverflow.com/questions/16621070/…

标签: android contextmenu expandablelistview android-viewbinder


【解决方案1】:

您也可以从 ContextMenuInfo 中获取孩子的 id,而不是依赖于视图。请参阅documentation,因为它应该有你想要的。

【讨论】:

  • 我得到了我想要的。我想知道这种方法到底有什么问题
  • 我正在使用 getExpandableListView.getChildAt(index)[where index=childpos] 来获取相应的视图,但它并不总是给我正确的视图。我以为它给了我想要的东西,但我猜测并非总是如此很多。
  • 如果您使用标准机制膨胀可扩展列表中的视图,这可能是问题所在。他们只会在第一次膨胀列表中的视图并将视图重用于后续项目。因此,您获取的视图几乎肯定不会包含您想要的数据:)。
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多