dimanche 29 janvier 2017

How can I use checkboxes and ExpandableListView?

I have an ExpandableListView, each category contains items with their respective checkboxes, but when I collapse a category or scroll the activity, the selection goes away. How can I keep my checkboxes checked for future use? And how can I use them?

Here's my item layout

<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
>

<ImageView
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:src="@mipmap/ic_launcher"
    android:layout_gravity="center_vertical"
    />

<TextView
    android:id="@+id/itemTv"
    android:textSize="14sp"
    tools:text="item"
    android:paddingLeft="8dp"
    android:layout_gravity="center_vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/itemCB" />

here's my adapter

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<Item>> listHashMap;

public ExpandableListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<Item>> listHashMap){
    this.context = context;
    this.listDataHeader = listDataHeader;
    this.listHashMap = listHashMap;
}

@Override
public int getGroupCount() {
    return listDataHeader.size();   //cantidad de grupos
}

@Override
public int getChildrenCount(int groupPosition) {
    return listHashMap.get(listDataHeader.get(groupPosition)).size();   //tamaño de grupo
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}

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

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

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

@Override  
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View view = convertView;
    String headerTitle = (String) getGroup(groupPosition);
    if(view == null){  
        LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.listgroup_header,null);
    }
    TextView lblListHeader = (TextView)view.findViewById(R.id.listgroupHeaderTv);
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle);
    return view;
}

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

    View view = convertView;
    ItemHolder holder = null;

    if(view == null){
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_item,null);
        holder = new ItemtHolder();
        holder.itemName = (TextView) view.findViewById(R.id.itemTv);
        holder.itemCheckBox = (CheckBox) view.findViewById(R.id.itemCB);
        //holder.itemCheckBox.setOnCheckedChangeListener((MainActivity) context);
        view.setTag(holder);
    }
    else{
        holder = (ItemHolder) view.getTag();

    }
    Item currentItem = (Item) getChild(groupPosition, childPosition);
    holder.itemName.setText(currentItem.getItemName());
    holder.itemCheckBox.setChecked(currentItem.isChecked());
    holder.itemCheckBox.setTag(currentItem);

    return view;
}

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

private static class ItemHolder{
    public TextView itemName;
    public CheckBox itemCheckBox;
}

}

I know it has to be something with the Holder but I don't know what, and I don't know how to use the checkListener and the expandableLV




Aucun commentaire:

Enregistrer un commentaire