BaseExpandableListAdapter无效

时间:2015-07-02 10:51:15

标签: android expandablelistview

我已为BaseExpandableListAdapter实施了自定义ExpandableListView。但是当我将适配器设置为列表时,它调用getGroupCount()方法,该方法返回4,但是其他方法永远不会被调用,并且永远不会创建行视图。你有什么主意吗?

PendientesAdapter:

public class PendientesAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    private Map<String, List<Purchase>> mGroupedReviews;
    private List<String> mGroups;

    public PendientesAdapter(Context mContext, Map<String, List<Purchase>> mGroupedReviews) {
        this.mContext = mContext;
        this.mGroupedReviews = mGroupedReviews;
        this.mGroups = new ArrayList<>(mGroupedReviews.keySet());
        Log.d("pendientesfragment", "KEYS: " + mGroups);
        Log.d("pendientesfragment", "VALUES: " + mGroupedReviews);
    }

    public void reloadData(Map<String, List<Purchase>> mGroupedReviews) {
        this.mGroupedReviews = mGroupedReviews;
        this.mGroups = new ArrayList<>(mGroupedReviews.keySet());
        this.notifyDataSetChanged();
    }

    @Override
    public int getGroupCount() {
        Log.d("pendientesfragment", "getGroupCount: " + mGroups.size());
        return mGroups.size();
    }

    @Override
    public int getChildrenCount(int i) {
        String group = getGroup(i);
        Log.d("pendientesfragment", "getChildrenCount: " + mGroupedReviews.get(group).size());
        return mGroupedReviews.get(group).size();
    }

    @Override
    public String getGroup(int i) {
        Log.d("pendientesfragment", "getGroup: " + mGroups.get(i));
        return mGroups.get(i);
    }

    @Override
    public Purchase getChild(int i, int i1) {
        String group = getGroup(i);
        return mGroupedReviews.get(group).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        Log.d("pendientesfragment", "getGroupView");

        GroupViewHolder holder;

        if (view == null) {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_reviews_group, null);
            holder = new GroupViewHolder();
            holder.name = (TextView) view.findViewById(R.id.name);
            view.setTag(view);
        } else {
            holder = (GroupViewHolder) view.getTag();
        }

        holder.name.setText(getGroup(i));

        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        Log.d("pendientesfragment", "getChildView");

        PurchaseViewHolder holder;

        if (view == null) {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_pendientes, null);
            holder = new PurchaseViewHolder();
            holder.name = (TextView) view.findViewById(R.id.name);
            view.setTag(holder);
        } else {
            holder = (PurchaseViewHolder) view.getTag();
        }

        Purchase purchase = getChild(i, i1);
        holder.name.setText(purchase.getComercio().getName());

        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private class GroupViewHolder {
        private TextView name;
    }

    private class PurchaseViewHolder {
        private TextView name;
    }
}

XML布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/dark"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ExpandableListView
        android:id="@+id/list"
        android:background="@color/green"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:2)

您的群组是否已展示?如果是这样,你试试点击一个群组吗? Booomm!

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to <package>.YourActivity$PendientesAdapter$GroupViewHolder

我认为你的问题只来自于这个演员:

public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        Log.d("pendientesfragment", "getGroupView");

        GroupViewHolder holder;

        if (view == null) {
            view = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_reviews_group, null);
            holder = new GroupViewHolder();
            holder.name = (TextView) view.findViewById(R.id.name);
            //view.setTag(view); <-- instead of
            view.setTag(holder);
        } else {
            holder = (GroupViewHolder) view.getTag();
        }

        holder.name.setText(getGroup(i));

        return view;
    }

Image of working app

希望它有所帮助!