0

Im new to android programming, the current architecture of my application is a Main Activity with a Fragment that fills the display. I am trying to create an ExpandableListView inside the Fragment which also fills the display. Everything works as i am expecting it, i think, however I cannot populate either the Child View or Group View.

In my Fragment Java class file i have;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ExpandableListView lv = (ExpandableListView) getView().findViewById(R.id.list);
    lv.setAdapter(new ParentLevel(null));
}

ParentLevel which extends BaseExpandableListAdapter has;

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

convertView returns null, but parent returns the ExpandableListView in my layout file.

I'm not sure how to work with this or what to do next, if i wanted to just have a simple TextView, for a title, and a the group arrow what would i do?

I plan to use an enum for the data, which I am already inspecting for getGroupCount() and getChildCount()

glend
  • 1,592
  • 1
  • 17
  • 36
  • 1
    You should read about what a ListView is and does. `converView` is always `null` until there is something that can be reused. Try reading here http://stackoverflow.com/q/11945563/1837367 – David Medenjak May 03 '17 at 18:54
  • @DavidMedenjak, Thank you for your comment. How would I create a simple `TextView` for use in my `ExpandableTextView`? – glend May 03 '17 at 19:06

1 Answers1

0

I was able to solve my problem with this peice of code;

private final LayoutInflater inf;
private FragmentActivity activity;

public ParentLevel(FragmentActivity act, MenuItem current) {
    activity = act;
    inf = LayoutInflater.from(act);
};    

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent)
{
    ViewHolder holder;

    if (convertView == null) {
        convertView = inf.inflate(R.layout.expandable_list_item, parent, false);

        holder = new ViewHolder();
        holder.text = (TextView) convertView.findViewById(R.id.text);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text.setText(activity.getString(siblings.get(groupPosition).getName()));

    return convertView;
}

and this class;

private class ViewHolder {
    TextView text;
}
glend
  • 1,592
  • 1
  • 17
  • 36