The question is simple, but I can't make it work. I have a expandable list with 3 header, they can be either populated or empty.
I wish to show a simple textview when you expand a group that is empty, I've been looking for a while but nothing seems to work. i can't use setEmpty() because this will only work if all the groups are empty.
I found a similar thread here: ExpandableListView - empty group message
If i try to do as the accepted answer suggested, so:
@Override
public int getChildrenCount(int groupPosition) {
if(this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size() != 0){
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
return 1;
}
I get an error when i try to expand the empty group ... And I don't think this would work anyway, for instance what will happen if I have a group with 1 child, will it be considerated as empty ??
EDIT:
Ok I solved this by always having an null object at the first place in mys child list, then when getchildrencount returns 1, I know it's actually empty and so I call the empty case of the switch. In the populated case of the switch i always hide the first element, this might not be the cleanest way to do this, but this works.
If you are using different layouts whether they are populated or empty, you can distinguish by overriding getChildType or getGroupType like this
private static final int CHILD_TYPE_COUNT = 2;
@Override
public int getChildType(int groupPosition, int childPosition) {
if(obj.isEmpty())
return 0;
else if(obj.isPopulated)
return 1;
}
@Override
public int getChildTypeCount() {
return CHILD_TYPE_COUNT;
}
Then you can use the corresponding layout to populate. I suggest you also use the ViewHolder-Pattern
static class PopulatedViewHolder {
//declare the elements of a populated view here
}
static class EmptyViewHolder {
//declare the elements of an empty view here
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
EmptyViewHolder emptyViewHolder = null;
PopulatedViewHolder populatedViewHolder = null;
//check if the type of your view is populated or empty and use the corresponding layout
int type = getChildType(groupPosition, childPosition);
if (convertView == null || (type == 0 && (convertView.getTag()) instanceof PopulatedViewHolder) || (type == 1 && (convertView.getTag()) instanceof EmptyViewHolder)) {
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
switch(type){
case 0:
//type is empty
emptyViewHolder = new EmptyViewHolder();
convertView = inflater.inflate(R.layout.empty_layout,
parent, false);
//...initialize your viewcomponents of the populated layout here...
convertView.setTag(emptyViewHolder);
break;
case 1:
//populated
populatedViewHolder = new PopulatedViewHolder();
convertView = inflater.inflate(
R.layout.populated_layout, parent, false);
//...initialize your viewcomponents of the populated layout here...
convertView.setTag(populatedViewHolder);
break;
}
}
else {
switch (type) {
case 0:
emptyViewHolder = (EmptyViewHolder)convertView.getTag();
break;
case 1:
PopulatedViewHolder = (PopulatedViewHolder)convertView.getTag();
break;
}
}
switch(type){
case 0:
//empty
//set the values of your empty view here
break;
case 1:
//populated
//set the values of your populated view here
break;
}
return convertView;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With