java - ExpandableView 上的错误没有出现任何内容

标签 java android android-studio expandablelistview

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

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

/image/dnM7Q.png

/image/eXJOz.png

/image/VmaUj.png

最佳答案

在将适配器分配给 ListView 之前,您必须先构造适配器:

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

关于java - ExpandableView 上的错误没有出现任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663654/

相关文章:

android - APK META_INF/NOTICE 中复制的重复文件

循环中的 Java-8 线程

java - 创建动态eclipse "Welcome"页面按钮

java - 动态改变JDialog的宽度

Android Studio - 终止进程重新启动应用程序?

android - 使用 Android Studio 在 Android 应用程序中编写测试用例

java - INSTALL_FAILED_MISSING_SHARED_LIBRARY 和意外的顶级异常

java - 如何从Java访问NexusDB

android - 选项卡上方的自定义 SherlockActionBar 布局

android - 如何在Android手机而不是模拟器中运行应用程序?