Android - ExpandableListView不显示任何内容

3

我刚刚接触到了ExpandableListView领域,希望在DrawerLayout中创建一个仅包含一个组和三个子项的简单列表。

这是我的适配器代码:

    public class DrawerShopAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> headerTitle = new ArrayList<>();
    private List<DrawerShopModel> drawerList = new ArrayList<>();

    public DrawerShopAdapter(Context context, List<DrawerShopModel> dataset) {
        this.context = context;
        this.drawerList.addAll(dataset);
        this.headerTitle.add("Toko Saya");
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.drawerList.get(childPosititon).drawer_txt;
    }

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

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.drawer_shop_item, null);
        }

        ImageView drawerIcon = (ImageView)convertView.findViewById(R.id.drawer_shop_item_img);
        OpenSansFont drawerTxt = (OpenSansFont) convertView.findViewById(R.id.drawer_shop_item_txt);

        drawerIcon.setImageDrawable(context.getResources().getDrawable(drawerList.get(childPosition).drawer_image));
        drawerTxt.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return  this.drawerList.size();
    }

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

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

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

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

        OpenSansFont listHeader = (OpenSansFont) convertView
                .findViewById(R.id.drawer_shop_header_title);
//        listHeader.setTypeface(null, Typeface.BOLD);
        listHeader.setText(headerTitle);

        return convertView;
    }

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

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

下面是我的DrawerShopModel

public class DrawerShopModel {
    public int drawer_image;
    public String drawer_txt;

    @Override
    public String toString() {
        return drawer_txt;
    }
}

我只是遵循MultiAutocompleteTextView方法来创建模型。

最后这是我在抽屉里调用它的方式:

public void setupShop(){
    expandableListView.setVisibility(View.VISIBLE);
    int images[] = { R.drawable.penjualan_gray, R.drawable.daftar_produk, R.drawable.keluhan_gray };
    String[] names = { "Penjualan", "Daftar Produk", "Keluhan"};

    List<DrawerShopModel> drawerShopModels = new ArrayList<>();

    for (int i = 0; i < 3; i++){
        DrawerShopModel drawerShopModel = new DrawerShopModel();
        drawerShopModel.drawer_image = images[i];
        drawerShopModel.drawer_txt = names[i];
        drawerShopModels.add(drawerShopModel);
    }

    drawerShopAdapter = new DrawerShopAdapter(this.getActivity(), drawerShopModels);
expandableListView.setAdapter(drawerShopAdapter);
}

抽屉里根本没有显示列表。我对适配器感到困惑,有人能引导我走向正确的方向吗?

2个回答

0

如果你正在尝试在抽屉布局中获取可展开列表,请尝试这种方法,这正是你想要的。

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private List<String> mExpandableListTitle;
    private Map<String, List<String>> mExpandableListDetail;
    private LayoutInflater mLayoutInflater;

    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       Map<String, List<String>> expandableListDetail) {
        mContext = context;
        mExpandableListTitle = expandableListTitle;
        mExpandableListDetail = expandableListDetail;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
            .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item, null);
        }
        TextView expandedListTextView = (TextView) convertView
            .findViewById(R.id.expandedListItem);
        expandedListTextView.setText(expandedListText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return mExpandableListDetail.get(mExpandableListTitle.get(listPosition))
            .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return mExpandableListTitle.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
            .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

查看更多,请点击抽屉式可扩展列表


什么?!所以在我的情况下,Map<String,List<DrawerShopModel>>是必需的吗? - Kevin Murvie
DrawerShopModel 是您的 POJO 类,您可以使用它。 - Aditya Vyas-Lakhan
是的,我是说我们必须使用Map来处理列表对吧??不相关的话题:顺便说一下,我喜欢POJO这个名字,POJO JOJO 哈哈哈 - Kevin Murvie
当然,如果您有任何问题,请告诉我。 - Aditya Vyas-Lakhan
成功显示了标题,但是内容没有显示出来。很奇怪,一个适配器需要项目计数> 0才能正确显示其内容? - Kevin Murvie
显示剩余4条评论

0

看起来你做的一切都是正确的,只是忘记在你的ExpandableListView中设置Adapter了。

在你的setupShop()方法中,你需要像这样设置你的Adapter

expandableListView.setAdapter(drawerShopAdapter);

哦,我把那个放在了setupShop()调用下面,为了更清晰明了,现在我会把它放进setupShop()里面,呵呵。 - Kevin Murvie
哦,我的错:D,你能否发布相同的XML文件,因为你的代码看起来一切正常。 - Satyen Udeshi
我漏掉了一些东西,我没有在适配器中放置组标题,因此没有给它任何内容显示,我将更新我的改进后的适配器。 - Kevin Murvie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接