可扩展列表视图显示子对象

时间:2016-07-21 19:02:01

标签: android listview object expandablelistview

我想给我的适配器一个对象,该对象具有对象的关键值和对象的arraylist。

例如

    Ingredient beef = new Ingredient("beef");
    Ingredient cheese = new Ingredient("cheese");
    Ingredient salsa = new Ingredient("salsa");
    Ingredient tortilla = new Ingredient("tortilla");
    Ingredient ketchup = new Ingredient("ketchup");
    Ingredient bun = new Ingredient("bun");





    Recipe taco = new Recipe("taco", Arrays.asList(beef, cheese, salsa, tortilla));
    Recipe quesadilla = new Recipe("quesadilla", Arrays.asList(cheese, tortilla));
    Recipe burger = new Recipe("burger", Arrays.asList(beef, cheese, ketchup, bun));

    recipes = Arrays.asList(taco, quesadilla, burger);

现在我如何让我的可扩展适配器显示组标题,例如taco,quesadilla和burger。然后当展开时,它显示每个的成分?两者都需要成为对象,因为下一步将是成分能够选择和取消选择某些成分并将其作为新对象传递。

我的适配器

public class ExpandableListAdapter extends BaseExpandableListAdapter {

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

//Keep track of checks
ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();

public ExpandableListAdapter(Context context, ArrayList<Recipe> listDataHeader,
                             HashMap<Recipe, List<String>> 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);
}

@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.list_item_create_team_child, null);
    }

    final CheckBox chk = (CheckBox) convertView.findViewById(R.id.checkBoxChild);

    chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                                        @Override
                                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                                if (isChecked == true){
                                                    //_listDataChild.get(childPosition)
                                                }
                                        }
                                    }
    );

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.teamNameChild);

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {



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

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.teamName);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(_listDataHeader.get(groupPosition).getName());

    return convertView;
}

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

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

}

配方对象:

public class Recipe implements ParentListItem {

    private String mName;
    private List<Ingredient> mIngredients;

    public Recipe(String name, List<Ingredient> ingredients) {
        mName = name;
        mIngredients = ingredients;
    }

    public String getName() {
        return mName;
    }

    @Override
    public List<?> getChildItemList() {
        return mIngredients;
    }

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

成分对象

public class Ingredient {

private String mName;

public boolean isSelected() {
    return isSelected;
}

public void setIsSelected(boolean isSelected) {
    this.isSelected = isSelected;
}

public String getmName() {
    return mName;
}

public void setmName(String mName) {
    this.mName = mName;
}

private boolean isSelected;

public Ingredient(String name) {
    mName = name;
}

public String getName() {
    return mName;
}

}

0 个答案:

没有答案