在expandableListView中的getChildView()处的IndexOutofBoundException

时间:2015-04-20 14:00:00

标签: android indexing arraylist expandablelistview indexoutofboundsexception

我正在添加三个名为All Homes所有图和All Commercial的子对象,我在运行时添加这三个对象,其中三个父项是Homes Plots和Commercial,所以在其他方面我要添加另外三个类别在我的父母作为child.i做了它,因为每当我加载我的expandableListview它加载我的数组的0索引上的子,但每当我尝试点击这些新创建的对象时,我得到indexoutofboundexception错误方法getChildView()因为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"
          }
        ]
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

请更改循环大小

你必须循环条件看起来像这样..

for(int i = 0; i&lt; Utils.propertyTypesList.size() - 1; i ++)

尺寸-1