【问题标题】:ExpandableListView ParentPosition not gettingExpandableListView ParentPosition 没有得到
【发布时间】:2013-07-31 19:07:47
【问题描述】:

大家好,我在这个主题上搜索了很多仍然无法解决这个问题。我在 android 中很新。我没有在我的 ExpandableListView 中获得 Parent 的位置,我得到的是 childPosition 的值,但不是 groupposition。

                catlist = (ExpandableListView) findViewById(R.id.category_list);
            final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
                    CatergoryActivity.this, groupList, expandableCategories);
            catlist.setAdapter(expListAdapter);

            catlist.setOnChildClickListener(new OnChildClickListener() {

                @Override
                public boolean onChildClick(ExpandableListView parent,
                        View v, int groupPosition, int childPosition,
                        long id) {
                    Intent nextact = new Intent(CatergoryActivity.this,
                            MobileActivity.class);

                    String childPosition = expListAdapter.getChild(groupPosition, childPosition);

                    System.out.println("childPosition------------------------>>>>>>>>>>>>"+childPosition);


                    nextact.putExtra("locId", locId);
                    nextact.putExtra("childpos",
                            childList.get(childPosition));
                    nextact.putExtra("grouppos", gruopPosition);

                    startActivity(nextact);
                    return false;
                }
            });

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    根据onChildClick的api引用描述,你已经有了父组的位置。

    public abstract boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 在 API 级别 1 中添加

    单击此可展开列表中的子项时调用的回调方法。

    参数

    • parent - 点击发生的 ExpandableListView
    • v - 可展开列表/ListView 中被点击的视图
    • groupPosition - 包含被点击子项的组位置
    • childPosition - 组内的子位置
    • id - 被点击的孩子的行 id

    返回 如果点击被处理则为真

    已编辑: onChildClick 中有两个您需要的位置 - parentchild。 父定义组的位置,子定义组内的位置。 adapter.getChild(groupPos, childPos) 返回与指定子项相关的对象。

        catlist.setOnChildClickListener(new OnChildClickListener() {
    
                @Override
                public boolean onChildClick(ExpandableListView parent,
                        View v, int groupPosition, int childPosition,
                        long id) {
    
                    System.out.println("childPosition: " + childPosition);
                    System.out.println("groupPosition: " + groupPosition);
    
                    // only if you need related data
                    Object itemData = expListAdapter.getChild(groupPosition, childPosition);
    
    
                }
            });
    

    如果你需要得到这个itemData值,你应该在你的适配器中实现getChild()方法。

    【讨论】:

    • 感谢您的回复,但您能告诉我在变量中获取该位置的代码是什么吗?就像我有 String childPosition = expListAdapter.getChild(groupPosition, childPosition);跨度>
    • 谢谢..实际上我是在询问该位置的值..因为我必须将值作为字符串发送到新活动
    • @user2384710 我已经编辑了答案,您应该重写适配器的 getChild() 方法以根据 ti 项目组和子位置获取正确的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2019-03-01
    相关资源
    最近更新 更多