我的Android项目中有一个expandable listview
,无法将子数据与群组相关联。我使用的是ArrayList Hashmaps
,但它们未分类。我从一个源获取Group数据,而从另一个源获取子数据。
有一个" ID"两个组中的值(键/值)和孩子们可以用它来将孩子与群体联系起来吗?或者我是否需要将组/子数据重新打包为已排序的List
,就像我看到很多可扩展的listview教程/示例一样?在此先感谢您的帮助。
public class LazyAdapter extends BaseExpandableListAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> groups;
private ArrayList<HashMap<String, String>> child;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
private DatabaseHandler dbh;
int flag;
static class RowHolder {
TextView name;
TextView id;
TextView reference;
TextView vicinity;
TextView lastcheck;
TextView number_checkins;
TextView user;
ImageView user_image;
ImageView checkin;
}
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d,
ArrayList<HashMap<String, String>> e) {
activity = a;
groups = d;
child = e;
inflater = (LayoutInflater)
activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
dbh = new DatabaseHandler(activity);
}
public void clearData() {
// clear the data
groups.clear();
}
public Object getChild(int groupPosition, int childPosition) {
return ((HashMap<String, String>)child.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return ((HashMap<String, String>)child.get(groupPosition)).size();
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View
convertView, ViewGroup parent) {
RowHolder holder = null;
if(convertView == null) {
vi = inflater.inflate(R.layout.child_view, null);
holder = new RowHolder();
holder.user_image = (ImageView) convertView.findViewById(R.id.user_image);
holder.user = (TextView) convertView.findViewById(R.id.user);
convertView.setTag(holder);
} else {
holder = (RowHolder) convertView.getTag();
}
HashMap<String, String> userList;
userList = child.get(groupPosition);
imageLoader.DisplayImage(userList.get(MainActivity.KEY_USERIMAGE),
holder.user_image);
holder.user.setText(userList.get(MainActivity.KEY_USER));
return convertView;
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View
convertView,ViewGroup parent) {
View vi = convertView;
RowHolder holder = null;
if (convertView == null) {
vi = inflater.inflate(R.layout.group_view, null);
holder = new RowHolder();
holder.number_checkins = (TextView) vi.findViewById(R.id.number_checkins);
holder.id = (TextView) vi.findViewById(R.id.id);
holder.name = (TextView) vi.findViewById(R.id.name);
holder.vicinity = (TextView) vi.findViewById(R.id.vicinity);
holder.lastcheck = (TextView) vi.findViewById(R.id.lastcheck);
holder.checkin = (ImageView) vi.findViewById(R.id.check_in);
vi.setTag(holder);
} else {
holder = (RowHolder) vi.getTag();
}
HashMap<String, String> placeList;
placeList = groups.get(groupPosition);
holder.id.setText(placeList.get(MainActivity.KEY_ID));
holder.name.setText(placeList.get(MainActivity.KEY_NAME));
holder.vicinity.setText(placeList.get(MainActivity.KEY_VICINITY));
holder.lastcheck.setText(placeList.get(MainActivity.KEY_LASTCHECK));
holder.number_checkins.setText(placeList.get(MainActivity.KEY_NUMBERCHECKINS));
return vi;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}