expandableview出错没有出现

时间:2015-09-19 02:50:41

标签: java android android-studio expandablelistview

My Fragment没有为我的可扩展视图显示任何内容。也没有错误。我不知道哪里出了问题所以我需要帮助有人能让我知道我的错误吗?页面上没有错误/没有任何崩溃

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);
    }
}

http://i.stack.imgur.com/dnM7Q.png

http://i.stack.imgur.com/eXJOz.png

http://i.stack.imgur.com/VmaUj.png

1 个答案:

答案 0 :(得分:1)

您必须在之前构建适配器,并将其分配给ListView

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