1

I wrote an implementation of BaseExpandableListAdapter which overrides as well the getChildType and getChildView methods. There are 10 Groups, 4 of them have children.

The following implementation works on the emulator and on tested htc devices. That means:
When I open GROUP_W visually, the convertView paramter of getChildView(...) is correctly null the first time and reuses the view later.

This implementation does not work on Samsungs Galaxy S3 and the Galaxy Tab. That means: When I open GROUP_W visually, the convertView paramter of getChildView(...) is not null the first time and provides a wrong view which leads further to an exception.

@Override
public int getChildType(int groupPosition, int childPosition) {
  switch (groupPosition) {
    case GROUP_W:
      return 0;
    case GROUP_X:
    case GROUP_Y:
      return 1;
    case GROUP_Z:
      return 2;
    default:
      // actually not used
      return super.getChildType(groupPosition, childPosition);
  }
}

@Override
public int getChildTypeCount() {
  return 3;
}

When i change the implementation to following, without seeing any logical reason, then it works on the Samsung devices as well.

@Override
public int getChildType(int groupPosition, int childPosition) {
  switch (groupPosition) {
    case GROUP_W:
      return 1;
    case GROUP_X:
    case GROUP_Y:
      return 2;
    case GROUP_Z:
      return 3;
    default:
      return super.getChildType(groupPosition, childPosition);
  }
}

@Override
public int getChildTypeCount() {
  return 4;
}

Does anyone have this strange behaviour as well, or does anybody understand what i am doing or what is going wrong? Thanks a lot ;)

Edit: Added getChildView()

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  Context context = parent.getContext();
  switch (groupPosition) {
    case GROUP_W:
      X x = arrayX[childPosition];
      return ListItemFactory.getTwoLineListItemTextIndicatorTopRight(context, x.getString1(), x.getString2(), x.getString3(), convertView);
    case ...
    default:
      return null;
  }
}
public static View getTwoLineListItemTextIndicatorTopRight(Context context, CharSequence line1, CharSequence line2,
    CharSequence indicator, View convertView) {
  View view;
  if (convertView == null) {
    view = View.inflate(context, R.layout.two_line_list_item_textindicator_topright, null);
  }
  else {
    view = convertView;
  }

  // here happens a null pointer exception because textView1 is not available in the wrong convert view

  ((TextView) view.findViewById(R.id.textView1)).setText(line1);
  ((TextView) view.findViewById(R.id.textView2)).setText(line2);
  if (indicator == null || indicator == "") {
    ((TextView) view.findViewById(R.id.textViewIndicator)).setVisibility(View.GONE);
  }
  else {
    TextView textView = ((TextView) view.findViewById(R.id.textViewIndicator));
    textView.setText(indicator);
    textView.setVisibility(View.VISIBLE);
  }
  return view;
}
Diego Frehner
  • 2,396
  • 1
  • 28
  • 35
  • I suspect the problem is in `getChildView`, can you add the code for that method? – smok Sep 18 '12 at 10:30
  • maybe getViewType() is interfering somewhere? :/ – Sherif elKhatib Sep 18 '12 at 10:36
  • returning 0 in `getChildType` seems to have a special meaning ("0 for any group or child position, since only one child type count is declared."). Try child type count of 3 but keep the types at 1-3. – zapl Sep 18 '12 at 11:20
  • Why do you assume that 0 has a special meaning? Why does it behave different, just for a random reason? It is not possible to declare more than one child type count. Are you confusing it with getChildCount? – Diego Frehner Sep 18 '12 at 11:44
  • The [`getChildType` documentation](http://developer.android.com/reference/android/widget/BaseExpandableListAdapter.html#getChildType%28int,%20int%29) has the "0 for any group.." part which makes me think that 0 means "I don't care" and you get a random `convertView`. And you do `case GROUP_W:return 0;` in your first example but `return 1` in the second. The numbers returned from `getChildType` don't have to be `0..getChildCount()-1`. – zapl Sep 18 '12 at 12:16
  • Thats really suspicious. I did not see the documentation of this method before, because i think it is newly documented. When i researched about the convert views this methods were undocumented. I understand it the same way like you do. It still does not explain the different behaviour, but maybe samsung modified here something to the original behaviour which seems not to be like the the documentation says. Can you add it as an answer please so i can accept it. – Diego Frehner Sep 18 '12 at 12:29

0 Answers0