Sunday, June 9, 2013

[android help] Add header in ChildView from ExpandableListView (Android)


Add header in ChildView from ExpandableListView (Android)



I'm trying to find a solution for my answer through the Internet but I can't find a good one. I looked this topic: Android ExpandableListView Child Headers but it doesn't work for me.


What I have now (Screen):



And my code is:



private void fillData() {
ExpandableListView lv;
lv = (ExpandableListView) getActivity().findViewById(R.id.grades_view);
lv.setItemsCanFocus(false);
lv.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);

View view = View.inflate(getActivity(), R.layout.list_header, null);
lv.addHeaderView(view, null, false);

dbh = new DatabaseHelper(getActivity());
MyExpandableListAdapter mAdapter = new MyExpandableListAdapter(
getActivity(), dbh);
mAdapter.synchronize();
lv.setAdapter(mAdapter);
registerForContextMenu(lv);
}

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

private List mSubjects = new ArrayList();
private List> mCourse = new ArrayList>();
private DatabaseHelper dbh;
private LayoutInflater inflater;

public MyExpandableListAdapter(Context context, DatabaseHelper dbh) {
this.dbh = dbh;
this.inflater = LayoutInflater.from(context);
}

public void synchronize() {

mSubjects = dbh.getSubjects();

for (Subject s : mSubjects) {
mCourse.add(dbh.getCourses(s));
}
}

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

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

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

Course c = (Course) getChild(groupPosition, childPosition);

if (convertView == null)
convertView = inflater.inflate(R.layout.list_item_child,
parent, false);

TextView txt = (TextView) convertView.findViewById(R.id.code);
txt.setText(c.getCode());
TextView txt1 = (TextView) convertView.findViewById(R.id.ects);
txt1.setText(String.valueOf(c.getEcts()));
TextView txt2 = (TextView) convertView.findViewById(R.id.grade);

if (c.getGrade() == -1) {
txt2.setText("-");
} else {
txt2.setText(String.valueOf(c.getGrade()));
}

return convertView;
}

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

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

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

@Override
public long getGroupId(int groupPosition) {
return mSubjects.get(groupPosition).hashCode();
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

Subject s = (Subject) getGroup(groupPosition);

if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, parent,
false);

TextView txt1 = (TextView) convertView.findViewById(R.id.name);
txt1.setText(String.valueOf(s.getName()));
TextView txt2 = (TextView) convertView.findViewById(R.id.semester);
txt2.setText(String.valueOf(s.getSemester()));

return convertView;

}

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

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}


As we can see, to put the first-level header (Name and Sem) I used:



View view = View.inflate(getActivity(), R.layout.list_header, null);
lv.addHeaderView(view, null, false);


But I don't know how to do the same with the children (a header with Code, ECTS and Grade).


Thank you in advance.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...