I have an expandable list view:public class ExListViewAdapter extends BaseExpandableListAdapter {    private Context context;    private ExpandableListView exv;    private String[] sportList;    private final int AVAILABLE = 3;    private String[][] childList = {            { Fifa Net Width, Fifa Penalty Kick },            { NBA Free Throw Line, NBA 3pt Line },            { Pitching Mound, Base Distance },            { Available in the Paid Version },            { Available in the Paid Version },            { Available in the Paid Version },            { Available in the Paid Version } };    private InputValues iv;    private int lastExpandedGroupPosition;    public ExListViewAdapter(Context context, ExpandableListView e) {        this.context = context;        this.iv = (InputValues) context;        this.sportList = iv.getResources().getStringArray(                R.array.sportsCategories);        this.exv = e;    }    @Override    public View getChildView(final int groupPos, final int childPos,            boolean isLastChild, View convertView, ViewGroup parent) {        Button tv = new Button(context);        tv.setText(childList[groupPos][childPos]);        if (groupPos < AVAILABLE) {            tv.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    iv.setPresetType(sportList[groupPos],                            childList[groupPos][childPos]);                }            });        } else {            tv.setEnabled(false);        }        return tv;    }    @Override    public void onGroupExpanded(int groupPosition) {        if (groupPosition != lastExpandedGroupPosition) {            exv.collapseGroup(lastExpandedGroupPosition);        }        super.onGroupExpanded(groupPosition);        lastExpandedGroupPosition = groupPosition;    }    @Override    public int getChildrenCount(int groupPos) {        return childList[groupPos].length;    }    @Override    public int getGroupCount() {        return sportList.length;    }    @Override    public long getGroupId(int groupPos) {        return groupPos;    }    @Override    public View getGroupView(int groupPos, boolean isExpanded,            View convertView, ViewGroup parent) {        TextView tv = new TextView(context);        tv.setText(sportList[groupPos]);        tv.setPadding(55, 0, 0, 0);        tv.setTextSize(25);        tv.setTextColor(Color.WHITE);        return tv;    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return null;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return 0;    }    @Override    public Object getGroup(int groupPosition) {        return null;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public boolean isChildSelectable(int groupPosition,            int childPosition) {        return false;    }}I feel like parts of the code, like the childlist, can be replaced with something or fixed up. Does anyone know of a shorter/more efficient way to create an expandable list view? EDIT:Due to some people answering, I have decided to put up a more recent version of the code, with their corrections.public class ExpandableListViewAdapter extends        BaseExpandableListAdapter {    private Context context;    private ExpandableListView expListView;    private final int FREE_LIMIT = 3;    private String[] sportList;    private String[][] childList;    private Input inp;    private int lastExpandedGroupPosition;    private Resources res;    public ExpandableListViewAdapter(Context context,            ExpandableListView expListView) {        this.context = context;        this.inp = (Input) context;        this.res = inp.getResources();        this.sportList = res.getStringArray(R.array.sportsCategories);        this.childList = new String[sportList.length][10];        populateChildList();        this.expListView = expListView;    }    private void populateChildList() {        for (int x = 0; x < sportList.length; x++) {            childList[x] = Presets.getSportsPresets(sportList[x]);        }    }    @Override    public View getChildView(final int groupPos, final int childPos,            boolean isLastChild, View convertView, ViewGroup parent) {        final String CHILD = childList[groupPos][childPos];        final Button BTN = new Button(context);        final int BTN_BG_COLOR = res.getColor(R.color.steelblue);        BTN.setText(CHILD);        BTN.setBackgroundColor(BTN_BG_COLOR);        BTN.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                BTN.setBackgroundColor(res.getColor(R.color.gray));                inp.setPresetType(sportList[groupPos], CHILD);                inp.mSlidingLayer.closeLayer(true);                expListView.collapseGroup(lastExpandedGroupPosition);            }        });        if (groupPos > FREE_LIMIT - 1)            BTN.setEnabled(false);        return BTN;    }    @Override    public void onGroupExpanded(int groupPosition) {        if (groupPosition != lastExpandedGroupPosition)            expListView.collapseGroup(lastExpandedGroupPosition);        super.onGroupExpanded(groupPosition);        lastExpandedGroupPosition = groupPosition;    }    @Override    public int getChildrenCount(int groupPos) {        return childList[groupPos].length;    }    @Override    public int getGroupCount() {        return sportList.length;    }    @Override    public long getGroupId(int groupPos) {        return groupPos;    }    @Override    public View getGroupView(int groupPos, boolean isExpanded,            View convertView, ViewGroup parent) {        TextView tView = new TextView(context);        tView.setText(sportList[groupPos]);        tView.setPadding(60, 10, 20, 10);        // tv.setBackgroundColor(iv.getResources().getColor(        // R.color.steelgreen));        tView.setTextColor(Color.WHITE);        tView.setTextSize(30f);        tView.setGravity(Gravity.RIGHT);        return tView;    }    @Override    public Object getChild(int groupPosition, int childPosition) {        return null;    }    @Override    public long getChildId(int groupPosition, int childPosition) {        return 0;    }    @Override    public Object getGroup(int groupPosition) {        return null;    }    @Override    public boolean hasStableIds() {        return false;    }    @Override    public boolean isChildSelectable(int groupPosition,            int childPosition) {        return false;    }}
Shorten/optimize an ExpandableListView
java;optimization;android
Data SourcesYou have three different sources of your data....sportList is an array of the group values, and it is pulled from R.array.sportsCategories.private final int AVAILABLE = 3; is a constant defined in the class.childList is declared as an array in the class, but has extra data.The reality is that AVAILABLE should be calculated from sportList, and the childList should be pulled from R as well.Naming ConventionButton tv = new Button(context);tv is a bad name, and it is because you copy/paste code from a TextView based method. In IDE's it is really easy to rename variables, even if the code is copy/pasted. There is no excuse ... ;-)EfficiencyApart from the above, I can't see any significant inefficiencies that need to be rectified. This code is run very few times, and is a low priority for efficiency.