【问题标题】:IndexOutofBoundException at getChildView() in expandableListViewexpandableListView 中 getChildView() 处的 IndexOutofBoundException
【发布时间】:2015-06-27 07:19:11
【问题描述】:

我要添加三个子对象,名称为 All Homes All plots 和 All Commercial在我的父母中作为孩子。我已经完成了它,因为每当我加载我的 expandableListview 时,它会将孩子加载到我的数组的 0 索引上,但是每当我尝试点击这些新创建的对象时,我在方法 getChildView() 处得到 indexoutofboundexception 错误,因为 IndexOutOfBoundException 无效索引 7 的大小为 7,当我单击子数据填充时调试器显示此错误,当我导航返回时,它在此方法中显示此 indexoutofBoundException 错误。 我修改后的代码是

private void displayListListView(){
    final ExpandableListView listview = (ExpandableListView)mView. findViewById(R.id.expandableListView);
    final ExpendableAdapter expendableAdapter = new ExpendableAdapter();
    //ArrayList<PropertyParent> childs = new ArrayList<PropertyParent>();
    //PropertyParent parent =   new PropertyTypeInfo().new PropertyParent();
    PropertyTypeInfo property = new PropertyTypeInfo();

    for(int i=0;i<Utils.propertyTypesList.size();i++)
    {
    //  if( Utils.propertyTypesList.size()>0)
        //{

        property = Utils.propertyTypesList.get(i);
        //if(property.getParentId()=="1" && property!=null){
        if(parent==null)
        {


             parent =   new PropertyTypeInfo().new PropertyParent();

            parent.setTypeId("9,8,21,22,20,24,25");
            parent.setTitle(value.concat(property.getTitle()));
            parent.setTitleAlt2("All Houses");
            parent.setParentId("1");
            parent.setTitleAlt1("All House");
            childs= property.getChilds();
            childs.add(0, parent);
            }
        //}

            else if(parent.getTypeId()=="9,8,21,22,20,24,25"){
                parent =    new PropertyTypeInfo().new PropertyParent();

                parent.setTypeId("12,23,11,19,26,27");
                parent.setTitle(value.concat(property.getTitle()));
                parent.setTitleAlt2("All Plots");
                parent.setParentId("2");
                parent.setTitleAlt1("Residential Plot");
                childs= property.getChilds();
                childs.add(0, parent);

            }

            else if(parent.getTypeId()== "12,23,11,19,26,27")
            {
                parent =    new PropertyTypeInfo().new PropertyParent();

                parent.setTypeId("13,15,17,14,16,18");
                parent.setTitle(value.concat(property.getTitle()));
                parent.setTitleAlt2("All Commercial");
                parent.setParentId("3");
                parent.setTitleAlt1("Commercial");
                childs= property.getChilds();
                childs.add(0,parent);



            }

        //else
        //{




        //}





    }
//  }

    expendableAdapter.notifyDataSetChanged();

    listview.setAdapter(expendableAdapter);
    //expendableAdapter.notifyDataSetChanged();
    listview.setOnChildClickListener(new OnChildClickListener() {

而我的 getChildView 方法是

@Override
    public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null){
            convertView =  inflater.inflate(R.layout.radio_item, parent, false);
        }
        RadioButton rbSelect = (RadioButton) convertView
                .findViewById(R.id.radioButton1);
        rbSelect.setText(Utils.propertyTypesList.get(groupPosition).getChildList().get(childPosition).getTitle());
        if(selectedIndexChild == childPosition && selectedIndexParent == groupPosition){
            rbSelect.setChecked(true);
        }
        else{
            rbSelect.setChecked(false);
        }
        return convertView;
    }
}

我在 Json 中的数据是

 "meta": {
    "status": "200",
    "mesaage": "Data Successfully Retrieved"
  },
  "response": {
    "type": [
      {
        "type_id": "1",
        "title": "Homes",
        "title_alt2": "Homes",
        "parent_id": "",
        "title_alt1": "Home",
        "child_list": "9,8,21,22,20,24,25",
        "childs": [
          {
            "type_id": "9,8,21,22,20,24,25",
            "title": "All Homes",
            "title_alt2": "All  Houses",
            "parent_id": "1",
            "title_alt1": "All House"
          },
          {
            "type_id": "9",
            "title": "Houses",
            "title_alt2": "Houses",
            "parent_id": "1",
            "title_alt1": "House"
          },
          {
            "type_id": "8",
            "title": "Flats",
            "title_alt2": "Flats & Apartments",
            "parent_id": "1",
            "title_alt1": "Flat"
          },
          {
            "type_id": "21",
            "title": "Upper Portions",
            "title_alt2": "Upper Portions",
            "parent_id": "1",
            "title_alt1": "Upper Portion"
          },
          {
            "type_id": "22",
            "title": "Lower Portions",
            "title_alt2": "Lower Portions",
            "parent_id": "1",
            "title_alt1": "Lower Portion"
          },
          {
            "type_id": "20",
            "title": "Farm Houses",
            "title_alt2": "Farm Houses",
            "parent_id": "1",
            "title_alt1": "Farm House"
          },
          {
            "type_id": "24",
            "title": "Rooms",
            "title_alt2": "Rooms",
            "parent_id": "1",
            "title_alt1": "Room"
          },
          {
            "type_id": "25",
            "title": "Penthouse",
            "title_alt2": "Penthouse",
            "parent_id": "1",
            "title_alt1": "Penthouse"
          }
        ]
      },
      {
        "type_id": "2",
        "title": "Plots",
        "title_alt2": "Plots",
        "parent_id": "",
        "title_alt1": "Plot",
        "child_list": "12,23,11,19,26,27",
        "childs": [
          {
            "type_id": "12,23,11,19,26,27",
            "title": "All Plots",
            "title_alt2": "All Plots",
            "parent_id": "2",
            "title_alt1": "Residential Plot"
          },
          {
            "type_id": "12",
            "title": "Residential Plots",
            "title_alt2": "Plots",
            "parent_id": "2",
            "title_alt1": "Residential Plot"
          },
          {
            "type_id": "23",
            "title": "Plot Files",
            "title_alt2": "Plots",
            "parent_id": "2",
            "title_alt1": "Plot File"
          },
          {
            "type_id": "11",
            "title": "Commercial Plots",
            "title_alt2": "Commercial Plots",
            "parent_id": "2",
            "title_alt1": "Commercial Plot"
          },
          {
            "type_id": "19",
            "title": "Agricultural Land",
            "title_alt2": "Agricultural Land",
            "parent_id": "2",
            "title_alt1": "Agricultural Land"
          },
          {
            "type_id": "26",
            "title": "Plot Forms",
            "title_alt2": "Plot Forms",
            "parent_id": "2",
            "title_alt1": "Plot Form"
          },
          {
            "type_id": "27",
            "title": "Industrial Land",
            "title_alt2": "Industrial Land",
            "parent_id": "2",
            "title_alt1": "Industrial Land"
          }
        ]
      },
      {
        "type_id": "3",
        "title": "Commercial",
        "title_alt2": "Commercial",
        "parent_id": "",
        "title_alt1": "Commercial Property",
        "child_list": "13,15,17,14,16,18",
        "childs": [
          {
            "type_id": "13",
            "title": "Offices",
            "title_alt2": "Offices",
            "parent_id": "3",
            "title_alt1": "Office"
          },
          {
            "type_id": "15",
            "title": "Shops",
            "title_alt2": "Shops",
            "parent_id": "3",
            "title_alt1": "Shop"
          },
          {
            "type_id": "17",
            "title": "Warehouses",
            "title_alt2": "Warehouses",
            "parent_id": "3",
            "title_alt1": "Warehouse"
          },
          {
            "type_id": "14",
            "title": "Factories",
            "title_alt2": "Factories",
            "parent_id": "3",
            "title_alt1": "Factory"
          },
          {
            "type_id": "16",
            "title": "Buildings",
            "title_alt2": "Buildings",
            "parent_id": "3",
            "title_alt1": "Building"
          },
          {
            "type_id": "18",
            "title": "Other",
            "title_alt2": "Other",
            "parent_id": "3",
            "title_alt1": "Other"
          }
        ]
      }
    ]
  }
}

【问题讨论】:

  • 旁注:parent.getTypeId()=="9,8,21,22,20,24,25"。将 == 替换为 .equals。另外,为什么要在 childList 中添加父级?
  • @carnal 我在哪里工作,他们已经根据网络服务从 url 获取数据,但现在他们想硬编码三个类别 All home、All Plots、All Commercial,以便在所有家庭中所有家庭孩子的come and in All Plots plots child come and in future if they want to add another function they can hard code it to get results without change in their API.
  • 又来了 java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7 at Fragments.Types$ExpendableAdapter.getChildView(Types.java:254)

标签: android indexing arraylist expandablelistview indexoutofboundsexception


【解决方案1】:

请更改您的循环大小

你必须像这样循环条件..

for(int i=0;i

尺寸-1

【讨论】:

  • 如果我将其设为 Utils.propertyTypesList.size()-1 那么数组将无法正确遍历,就像它会到达 0 和 1 索引并且会留下 2 索引,因此只有所有房屋的所有地块都是显示但仍然存在错误,即在单击单选按钮时填充数据后我来了 indexoutofbound 异常错误并应用程序崩溃
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-31
  • 2019-11-14
相关资源
最近更新 更多