IndexOutofBoundException getChildView()in expandableListView

问题描述:

我添加了三个子对象,名称为All Homes所有地块和所有商业地产,我在运行时做这些事情时,我将这三个对象与三个父母一起添加到Home Plots和Commercial中,条款我在我的父母添加三个类别作为child.i已完成与它每当我加载我的expandableListview它加载子数组的索引我的数组,但每当我尝试点击这些新创建的对象我得到indexoutofboundexception错误方法getChildView()作为IndexOutOfBoundException无效索引7大小是7和调试器显示此错误,当我点击子数据填充,当我导航回它显示此indexoutofBoundException在此方法的错误。 我修改的代码是IndexOutofBoundException getChildView()in expandableListView

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" 
      } 
     ] 
     } 
    ] 
    } 
} 
+0

旁注:parent.getTypeId()==“9,8,21,22,20,24,25”。将==替换为.equals。另外,为什么你将父项添加到你的childList? – Carnal

+0

@carnal在我工作的时候,他们已经从基于web服务的url中获取数据,但是现在他们想要硬编码三个类别All Homes,All Plots,All Commercial,以便All Homes所有家庭孩子都来了,如果他们想要添加另一个功能,他们可以在不改变其API的情况下对其进行硬编码以获得结果,那么在将来和未来都会如此。 –

+0

它又来java.lang.IndexOutOfBoundsException:无效索引7,大小为7 \t at fragments.Types $ ExpendableAdapter.getChildView(Types.java:254) –

请改变你的循环大小

你要循环的情况看是这样..

的for(int i = 0;我< Utils.propertyTypesList.size() - 1;我++)

大小-1

+0

如果我使它Utils.propertyTypesList.size() - 1那么数组会没有正确的遍历它会去0和1索引,并会离开2索引,因此只有所有家庭的所有情节将显示,但错误仍然存​​在,当填充数据时点击单选按钮,我来bak indexoutofbound异常错误来和应用程序崩溃 –