自定义Expandablelistview分类数据

时间:2015-06-04 15:09:57

标签: android expandablelistview expandablelistadapter

我有一个包含数据的可扩展Listview,存储在List和Hashmap中。我的问题是,当我折叠和展开一个组时,可扩展的列表视图显示未排序的数据。我疯了!!!

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pebbo.yaa.R;

public class CustomExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<PojoAlgCat> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<PojoAlgCat, List<PojoAlg>> _listDataChild;

    Integer[] Algs_lite = {5, 19};
    Integer[] Algs_cat_lite = {1, 2};

    LinearLayout ll_row, ll_row_child;
    TextView lblListHeader;
    TextView txtListChild;

    public CustomExpandableListAdapter(Context context, List<PojoAlgCat> listDataHeader,
                                       HashMap<PojoAlgCat, List<PojoAlg>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

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

    public Object getChildTitle(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosititon).getName();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition).getId();
    }

    public int getCatId(int groupPosition) {
        return this._listDataHeader.get(groupPosition).getId();
    }

    public int getAlgId(int groupPosition, int childPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition)).get(childPosition).getId();
    }

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

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

        boolean exists = containsValue(Algs_lite, getAlgId(groupPosition, childPosition));

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);

            ll_row_child = (LinearLayout) convertView.findViewById(R.id.ll_row_child);
            txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);

            if(exists){
                //Log.i("Adapter1", "VALUE:::" + exists + ":::ID:::" + f.getId() + ":::NAME:::" + f.getName() + ":::DISPONIBLE");
                ll_row_child.setBackgroundColor(Color.WHITE);
                txtListChild.setTextColor(_context.getResources().getColor(R.color.DarkBlueBG));
            }else{
                //Log.e("Adapter1", "VALUE:::" + exists + ":::ID:::" + f.getId() + ":::NAME:::" + f.getName() + ":::NO DISPONIBLE");
                ll_row_child.setBackgroundColor(Color.LTGRAY);
                txtListChild.setTextColor(Color.GRAY);

            }
        }

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

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

    public Object getGroupTitle(int groupPosition) {
        return this._listDataHeader.get(groupPosition).getCategory();
    }

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

    @Override
    public long getGroupId(int groupPosition) {
        return this._listDataHeader.get(groupPosition).getId();
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroupTitle(groupPosition);

        boolean exists_alg = containsValue(Algs_cat_lite, getCatId(groupPosition));

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);

            ll_row = (LinearLayout) convertView.findViewById(R.id.ll_row);
            lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);

            if(exists_alg){
                //Log.i("Adapter1", "VALUE:::" + exists + ":::ID:::" + f.getId() + ":::NAME:::" + f.getName() + ":::DISPONIBLE");
                ll_row.setBackgroundColor(_context.getResources().getColor(R.color.BlueBG));
                lblListHeader.setTextColor(Color.WHITE);
            }else{
                //Log.e("Adapter1", "VALUE:::" + exists + ":::ID:::" + f.getId() + ":::NAME:::" + f.getName() + ":::NO DISPONIBLE");
                ll_row.setBackgroundColor(_context.getResources().getColor(R.color.DimGray));
                lblListHeader.setTextColor(_context.getResources().getColor(R.color.LightGrey));
            }
        }

        lblListHeader.setText(headerTitle);

        return convertView;
    }

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

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

    public static boolean containsValue(Integer[] arr, Integer targetValue) {
        Set<Integer> set = new HashSet<Integer>(Arrays.asList(arr));
        return set.contains(targetValue);
    }
}

我做错了什么? :(

0 个答案:

没有答案