Wen Feng
Wen Feng

Reputation: 27

Error on expandableview Nothing appearing

My Fragment didn't show anything for my expandable view. there's not error as well. and i don't know where went wrong so i need help Can someone let me know my error? There's no error on the page/didn't have any crashes as well

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listGroup;
    private HashMap<String, List<String>> listChild;

    public MyExpandableListAdapter(Context context, List<String> listGroup,
                                   HashMap<String, List<String>> listChild) {
        this.context = context;
        this.listGroup = listGroup;
        this.listChild = listChild;
    }

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

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

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }

        String textGroup = (String) getGroup(groupPosition);

        TextView textViewGroup = (TextView) convertView
                .findViewById(R.id.group);
        textViewGroup.setText(textGroup);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item_layout, null);
        }

        TextView textViewItem = (TextView) convertView.findViewById(R.id.item);

        String text = (String) getChild(groupPosition, childPosition);

        textViewItem.setText(text);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

}

public class Hotel extends Fragment {

    ExpandableListView expandableListView;
    MyExpandableListAdapter myExpandableListAdapter;
    List<String> groupList;
    HashMap<String, List<String>> childMap;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hotel_frag, container, false);

        init();
        expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
        expandableListView.setAdapter(myExpandableListAdapter);
        myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);

        return v;
    }
    private void init() {
        groupList = new ArrayList<String>();
        childMap = new HashMap<String, List<String>>();

        List<String> groupList0 = new ArrayList<String>();
        groupList0.add("groupList0 - 1");
        groupList0.add("groupList0 - 2");
        groupList0.add("groupList0 - 3");

        List<String> groupList1 = new ArrayList<String>();
        groupList1.add("groupList1 - 1");
        groupList1.add("groupList1 - 2");
        groupList1.add("groupList1 - 3");

        List<String> groupList2 = new ArrayList<String>();
        groupList2.add("groupList2 - 1");
        groupList2.add("groupList2 - 2");
        groupList2.add("groupList2 - 3");

        List<String> groupList3 = new ArrayList<String>();
        groupList3.add("groupList3 - 1");
        groupList3.add("groupList3 - 2");
        groupList3.add("groupList3 - 3");

        groupList.add("Group List 0");
        groupList.add("Group List 1");
        groupList.add("Group List 2");
        groupList.add("Group List 3");

        childMap.put(groupList.get(0), groupList0);
        childMap.put(groupList.get(1), groupList1);
        childMap.put(groupList.get(2), groupList2);
        childMap.put(groupList.get(3), groupList3);
    }
}

https://i.sstatic.net/dnM7Q.png

https://i.sstatic.net/eXJOz.png

https://i.sstatic.net/VmaUj.png

Upvotes: 0

Views: 32

Answers (1)

kris larson
kris larson

Reputation: 31015

You have to construct the adapter before you assign it to the ListView:

    init();
    myExpandableListAdapter = new MyExpandableListAdapter(Hotel.this.getActivity().getApplicationContext(), groupList, childMap);
    expandableListView = (ExpandableListView) v.findViewById(R.id.mylist);
    expandableListView.setAdapter(myExpandableListAdapter);

Upvotes: 1

Related Questions