我正在尝试将ExpandableListView与自定义适配器一起使用,但无法显示任何内容

时间:2016-06-14 20:12:11

标签: java android hashmap expandablelistview

我正在尝试使用ExpandableListView using this tutorial来显示物理常量及其信息。我已经为子项,父项和我希望列表显示的活动做了布局。我的数据存储在MyDataProvider类的HashMap中。我相信所有这一切都可以并且应该有效。

我认为造成这个问题的部分是适配器,我自己编写并包含在下面。问题是,当我进入此活动时,应用程序不会崩溃,但只显示一个完全空白的白色屏幕。如果能更好地告知任何人,我也会查看this tutorial

这是我的适配器:

package com.gmd.referenceapplication;

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;
import java.util.Map;

/**
 * Created by gmd on 6/13/2016.
 */
public class MyCustomAdapter extends BaseExpandableListAdapter {

    private Context context;
    private HashMap<String,ListViewItem> constantsHashMap;
    private List<String> constantList;

    public MyCustomAdapter(Context context, HashMap<String, ListViewItem> hashMap, List<String> list){

        constantsHashMap = hashMap;
        this.context = context;
        this.constantList = list;

    }



    public View getChildView(int groupPosition,
                             int childPosition,
                             boolean isLastChild,View convertView, ViewGroup parent)  {
        String childTitle = (String) getChild(groupPosition, childPosition).toString();
        if(convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_layout, parent, false);
        }
        TextView childTextView = (TextView) convertView.findViewById(R.id.textViewChild);
        childTextView.setText(childTitle);
        return convertView;


    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public int getGroupCount() {
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 0;
    }

    @Override
    public String getGroup(int groupPosition) {
        return constantList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return constantsHashMap.get(this.constantList.get(groupPosition));
                }


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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String groupTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.parent_layout, parent, false);
        }
        TextView parentTextView = (TextView) convertView.findViewById(R.id.textViewParent);
        parentTextView.setText(groupTitle);
        return convertView;
        }
    }

如果我在这里尝试显示列表的类包含它可能也很有用:

package com.gmd.referenceapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.gmd.referenceapplication.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class CommonConstants extends AppCompatActivity {
    View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_constants);


        MyDataProvider dp = new MyDataProvider();

        ExpandableListView view;
        view = (ExpandableListView) findViewById(R.id.expandableList);
        HashMap constantsHashMap;
        constantsHashMap = dp.getCommonMap();
        ArrayList constantsHashMapKeys = new ArrayList<String>(constantsHashMap.keySet());

        MyCustomAdapter adapter = new MyCustomAdapter(this, constantsHashMap, constantsHashMapKeys);
        view.setAdapter(adapter);










    }
}

我对此做了很多研究,我仍然不确定我做错了什么,所以我会感激任何帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

您在小组和儿童计数中返回0。此计数用于呈现组及其子组。试试这个

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

@Override
public int getChildrenCount(int groupPosition) {
    return 1; // Looks like you have only one child in each group, change this if not
}

答案 1 :(得分:1)

您应该提供正确数量的组和子项,而不是0。

@Override
public int getGroupCount() {
    return 0;
}

@Override
public int getChildrenCount(int groupPosition) {
    return 0;
}