【问题标题】:Error on expandableview Nothing appearingexpandableview 上的错误没有出现
【发布时间】:2015-12-16 06:57:23
【问题描述】:

我的 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

【问题讨论】:

    标签: java android android-studio expandablelistview


    【解决方案1】:

    您必须在将适配器分配给ListView 之前构建适配器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 2016-07-07
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 2018-01-16
      相关资源
      最近更新 更多