我正在添加三个名为All Homes所有图和All Commercial的子对象,我在运行时添加这三个对象,其中三个父项是Homes Plots和Commercial,所以在其他方面我要添加另外三个类别在我的父母作为child.i做了它,因为每当我加载我的expandableListview它加载我的数组的0索引上的子,但每当我尝试点击这些新创建的对象时,我得到indexoutofboundexception错误方法getChildView()因为IndexOutOfBoundException无效索引7大小为7,当我单击子数据时,调试器显示此错误,当我回到它时,它会在此方法中显示此indexoutofBoundException错误。我遇到错误的方法是
rbSelect.setText(Utils.propertyTypesList.get(groupPosition).getChildList().get(childPosition).getTitle());
我的代码是。
package com.zameen.fragments;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.json.JSONException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.RadioButton;
import android.widget.TextView;
import com.android.volley.Cache;
import com.android.volley.Cache.Entry;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.zameen.bl.AppController;
import com.zameen.bl.JsonParser;
import com.zameen.bo.PropertyTypeInfo;
import com.zameen.bo.PropertyTypeInfo.PropertyParent;
import com.zameen.dao.GetPropertyTypes;
import com.zameen.interfaces.PropertyTypeDidLoad;
import com.zameen.utils.GTMCall;
import com.zameen.utils.MyDialog;
import com.zameen.utils.Utils;
import com.zameen.utils.UtilsMethos;
import com.zameen.zameenapp.R;
public class Types extends Fragment implements OnClickListener{
private Context mContext;
private View mView;
public ArrayList<PropertyParent> childs;
public PropertyParent parent;
PropertyParent parent1;
String value=" All";
int selectedIndexChild = 0;
int selectedIndexParent = 0;
private Handler uiHandler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
Intent inetent = new Intent();
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_expendable_listview, container, false);
mView.setOnClickListener(this);
initItems();
return mView;
}
private void initItems() {
if(Utils.propertyTypesList != null){
displayListListView();
}else{
//load data from server
getPropertyTypes();
}
}
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().equals("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().equals("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() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
selectedIndexParent = groupPosition;
selectedIndexChild = childPosition;
expendableAdapter.notifyDataSetChanged();
Utils.userSearchInfo.setPropertyTypeName(Utils.propertyTypesList.get(groupPosition).getChildList().get(childPosition).getTitle());
Utils.userSearchInfo.setPropertyTypeParentIndex(selectedIndexParent);
Utils.userSearchInfo.setPropertyTypeChildIndex(selectedIndexChild);
Utils.userSearchInfo.setPropertyTypeId(Utils.propertyTypesList.get(groupPosition).getChildList().get(childPosition).getTypeId());
return true;
}
});
if(Utils.userSearchInfo.getPropertyTypeId() != null){
selectedIndexParent = Utils.userSearchInfo.getPropertyTypeParentIndex();
selectedIndexChild = Utils.userSearchInfo.getPropertyTypeChildIndex();
}else{
selectedIndexParent = -1;
selectedIndexChild = -1;
}
expendableAdapter.notifyDataSetChanged();
}
private class ExpendableAdapter extends BaseExpandableListAdapter{
LayoutInflater inflater;
public ExpendableAdapter() {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return Utils.propertyTypesList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return Utils.propertyTypesList.get(groupPosition).getChildList().size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return Utils.propertyTypesList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return Utils.propertyTypesList.get(groupPosition).getChildList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
if(convertView == null){
convertView = inflater.inflate(R.layout.list_header_item, parent, false);
}
TextView txtHeading = (TextView)convertView.findViewById(R.id.txtHeading);
txtHeading.setText(Utils.propertyTypesList.get(groupPosition).getTitle());
ExpandableListView eLV = (ExpandableListView) parent;
eLV.expandGroup(groupPosition);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@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;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
private PropertyTypeDidLoad propertyTypeDidLoad = new PropertyTypeDidLoad() {
@Override
public void onSuccess(ArrayList<PropertyTypeInfo> typeList) {
displayListListView();
}
@Override
public void onFailure(String message) {
MyDialog.showAlertDialog(mContext, message).show();
}
};
//get property types from server
private void getPropertyTypes(){
Cache cache = AppController.getInstance().getRequestQueue().getCache();
Entry entry = cache.get(Utils.ServerActions.BASE_URL + UtilsMethos.getPropertyTypeURL(mContext));
if(entry != null){
try {
final String data = new String(entry.data, "UTF-8");
if(data != null && data.length() > 0){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
String parseData = "";
try {
parseData = JsonParser.getValidJson(data, getString(R.string.property_type_key));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (VolleyError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Gson gson = new Gson();
PropertyTypeInfo[] locations = gson.fromJson(parseData,PropertyTypeInfo[].class);
Utils.propertyTypesList = UtilsMethos.convertArrayToList(locations);
uiHandler.post(new Runnable() {
@Override
public void run() {
propertyTypeDidLoad.onSuccess(Utils.propertyTypesList);
}
});
}
});
thread.start();
}else{
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else{
GetPropertyTypes propertyTypes = new GetPropertyTypes(mContext, propertyTypeDidLoad,true);
propertyTypes.getTypes();
}
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
GTMCall.pushOpenScreenEvent(mContext, mContext.getString(R.string.gtm_screen_search_property_type));
}
}
我的数据的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"
}
]
}
]
}
}
我的LogCat错误明细是
FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 7, size is 7
at java.util.ArrayList.throwIndexOutOfBoundsException
at java.util.ArrayList.get(ArrayList.java:304)
fragments.Types$ExpendableAdapter.getChildView()