【问题标题】:How to Determine the first and last child for a key in Hash Map?如何确定哈希映射中键的第一个和最后一个孩子?
【发布时间】:2016-06-26 06:37:19
【问题描述】:

我有一个可扩展列表视图。它的子节点是从哈希映射中填充的。我想要做的是我想从哈希映射中显示一个键的第一个和最后一个孩子的视图,然后为其他人隐藏。目前我正在尝试通过以下方式实现这一目标,但它不起作用。下面提到的是我的 EpandableListAdapter 类的 getChildView 方法。

@Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);


        LinearLayout titleLayout = null;
        LinearLayout sumTotalLayout = null;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.order_form_history_viewable, null);
            titleLayout = (LinearLayout) convertView.findViewById(R.id.llOrderHistroryTitles);
            sumTotalLayout = (LinearLayout) convertView.findViewById(R.id.llOrderHistroryTotals);
        }


        if (childPosition==0 && titleLayout != null) {
            titleLayout.setVisibility(View.VISIBLE);
        } else if (titleLayout != null) {

            titleLayout.setVisibility(View.GONE);
        }

      //  int childSize = this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
        if (isLastChild && sumTotalLayout != null) {
            sumTotalLayout.setVisibility(View.VISIBLE);
        } else if (sumTotalLayout != null) {
            sumTotalLayout.setVisibility(View.GONE);
        }
//        if (childPosition == childSize-1 && sumTotalLayout!=null){
//            sumTotalLayout.setVisibility(View.VISIBLE);
//        }else if (sumTotalLayout!=null){
//            sumTotalLayout.setVisibility(View.GONE);
//        }


        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.tvOrderlistBrand);

        txtListChild.setText(childText);
        return convertView;
    }

【问题讨论】:

  • 普通哈希映射没有保证顺序。 LinkedHashMap 确实如此,但那是另一回事。你期待什么订单? “它不工作”是什么意思(确切地)?
  • 你正在检查 if (convertView == null),但是当 covertView 不等于 null 时 else 部分在哪里??
  • 目前我正在检查子位置,如果 childPosition 参数为 0,那么它是第一个子位置。对于最后一个孩子,我正在考虑布尔参数 isLastChild。这个过程不起作用,我没有得到我想要的结果。请看上面提到的方法
  • 如果可以的话,会发生屏幕截图吗?
  • @AndroidKiller,我已经应用了它并且它现在正在工作。谢谢

标签: java android hashmap expandablelistview


【解决方案1】:

您正在检查if (convertView == null),但没有其他部分,即当 covertView 不等于 null 时。试着把它说出来,它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 2013-03-12
    • 2022-11-10
    • 1970-01-01
    • 2021-01-26
    • 2013-06-06
    • 1970-01-01
    • 2015-10-14
    相关资源
    最近更新 更多