My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

package com.beanie.example.list.adapter;

import java.util.ArrayList;

import com.beanie.example.list.R;
import com.beanie.example.list.classes.Bike;
import com.beanie.example.list.classes.Bus;
import com.beanie.example.list.classes.Car;
import com.beanie.example.list.classes.Vehicle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

@Override
public boolean areAllItemsEnabled()
{
return true;
}

private Context context;

private ArrayList<String> groups;

private ArrayList<ArrayList<Vehicle>> children;

public ExpandableListAdapter(Context context, ArrayList<String> groups,
ArrayList<ArrayList<Vehicle>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}

/**
* A general add method, that allows you to add a Vehicle to this list
*
* Depending on if the category opf the vehicle is present or not,
* the corresponding item will either be added to an existing group if it
* exists, else the group will be created and then the item will be added
* @param vehicle
*/
public void addItem(Vehicle vehicle) {
if (!groups.contains(vehicle.getGroup())) {
groups.add(vehicle.getGroup());
}
int index = groups.indexOf(vehicle.getGroup());
if (children.size() < index + 1) {
children.add(new ArrayList<Vehicle>());
}
children.get(index).add(vehicle);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

// Return a child view. You can load your custom layout here.
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(" " + vehicle.getName());

// Depending upon the child type, set the imageTextView01
tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
if (vehicle instanceof Car) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
} else if (vehicle instanceof Bus) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0);
} else if (vehicle instanceof Bike) {
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0);
}
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}

@Override
public int getGroupCount() {
return groups.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

// Return a group view. You can load your custom layout here.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group);
return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}

}

Change log

r20 by coomar.101 on Oct 6, 2010   Diff
Items are clickable
Go to: 
Project members, sign in to write a code review

Older revisions

r16 by coomar.101 on Sep 16, 2010   Diff
Comments
r15 by coomar.101 on Sep 16, 2010   Diff
Initial Commit
All revisions of this file

File info

Size: 4470 bytes, 139 lines
Powered by Google Project Hosting