1

My requirement is in ExpandbleListView view add CheckBox to parent item and that parent item CheckBox controls child check box how to get this? how to add check boxes any one give solution?

ExpandableList.java - Adapter class

public class ExpandableList extends BaseExpandableListAdapter {

Context Context;
private HashMap<String, List<String>> ChildItems;
private List<String> GroupItems;
HashMap<Integer,boolean[]> checkboxData;
CheckBox checkBox'
public ExpandableList(Context mContext, List<String> mGroupItems, HashMap<String, List<String>> mChildItems
                      ) {
    Context = mContext;
    ChildItems = mChildItems;
    GroupItems = mGroupItems;

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

@Override
public int getChildrenCount(int groupPosition) {
    return ChildItems.get(GroupItems.get(groupPosition)).size();
}

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return ChildItems.get(GroupItems.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) {
    String headerTitle = (String)getGroup(groupPosition);
    if (convertView==null)
    {
        LayoutInflater li = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.group_list,null);
    }
    TextView tv = (TextView)convertView.findViewById(R.id.groupTitle);
    tv.setTypeface(null, Typeface.BOLD);
    tv.setText(headerTitle);
    return convertView;
}

@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
   String listTitle = (String) getChild(groupPosition,childPosition);
    if (convertView==null)
    {
        LayoutInflater layoutInflater = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.items_list,null);
    }
    TextView tvv = (TextView)convertView.findViewById(R.id.items_list);
    tvv.setText(listTitle);
    tvv.setTypeface(null,Typeface.BOLD);
  CheckBox cb  = (CheckBox)convertView.findViewById(R.id.listCheckbox);

    return  convertView;
}

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

MainActivity.java

public class MainActivity extends AppCompatActivity {
ExpandableListView expandableListView;
private HashMap<String, List<String>>  ChildItemss;
private List<String> GroupItemss;
ExpandableList expandableList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listData();
    expandableListView = (ExpandableListView)findViewById(R.id.expandblelist);
    expandableList = new ExpandableList(this,GroupItemss,ChildItemss);
    expandableListView.setAdapter(expandableList);
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            return false;
        }
    });
    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(MainActivity.this, GroupItemss.get(groupPosition)+"Expanded", Toast.LENGTH_SHORT).show();
        }
    });
    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, final int groupPosition, final int childPosition, long id) {
       ChildViewHolder ch  = new ChildViewHolder();
            ch.childCheckbox  = (CheckBox)findViewById(R.id.listCheckbox);
           ch.childCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
               @Override
               public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   Toast.makeText(MainActivity.this, ChildItemss.get(childPosition)+"Expanded", Toast.LENGTH_SHORT).show();
               }
           });

            return true;
        }
    });
}
public void listData()
{
    GroupItemss = new ArrayList<String>();
    ChildItemss = new HashMap<String, List<String>> ();

    GroupItemss.add("Cricket");
    GroupItemss.add("India");

    List<String> cricket = new ArrayList<>();
  cricket.add("sachin");
    cricket.add("dhoni");
    cricket.add("yuvi");

   List<String> india = new ArrayList<String>();
    india.add("modi");
    india.add("pawankalyan");

    ChildItemss.put(GroupItemss.get(0),cricket);
    ChildItemss.put(GroupItemss.get(1),india);
}

items_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/items_listlayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="@color/colorPrimary"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:id="@+id/items_list"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listCheckbox"/>
</LinearLayout>

group_list.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/group_layout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textColor="@color/colorAccent"
android:id="@+id/groupTitle"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/groupCheckbox"/>
</LinearLayout>
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
A.asha
  • 45
  • 6
  • Can you post your xml files as well . Since to the group view you haven't added and checkbox . The view seems to have just TextView – Terril Thomas Jul 25 '16 at 07:01
  • The issue is you are giving event to click listner for the groupview . Rather you should handle checked listner for the for the checkbox inside it. – Terril Thomas Jul 25 '16 at 07:10
  • can u tell how to do that.where i can write i cant understand please post that code – A.asha Jul 25 '16 at 07:12

1 Answers1

0

Try this I tried for you . Modify as per your requirement.

package com.deeb.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.deeb.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by terril1 on 25/07/16.
 */
public class ExpandableList extends BaseExpandableListAdapter implements GroupItemClicked {

    Context mContext;
    private HashMap<String, List<String>> ChildItems;
    private List<String> GroupItems;
    HashMap<Integer, boolean[]> checkboxData;
    CheckBox checkBox;

    GroupItemClicked itemClicked;
    boolean itemcheckedTrue;
    int pos;

    public ExpandableList(Context mContext, List<String> mGroupItems, HashMap<String, List<String>> mChildItems
    ) {
        this.mContext = mContext;
        ChildItems = mChildItems;
        GroupItems = mGroupItems;
        itemClicked = this;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return ChildItems.get(GroupItems.get(groupPosition)).size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return ChildItems.get(GroupItems.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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.group_list, null);
            holder.lblTitle = (TextView) convertView.findViewById(R.id.groupTitle);
            holder.checkedItem = (CheckBox) convertView.findViewById(R.id.groupCheckbox);
            holder.lblTitle.setTypeface(null, Typeface.BOLD);
            holder.checkedItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //is chkIos checked?
                    if (((CheckBox) v).isChecked()) {
                        Log.e("TAG", "clicked");
                        itemClicked.itemclicked(groupPosition, true);
                        //Case 1
                    } else {

                    }
                    //case 2

                }
            });

            convertView.setTag(holder);
            holder.checkedItem.setTag(groupPosition);
        } else {
            holder = (ViewHolder) convertView.getTag();
            (holder).checkedItem.getTag();
        }
        holder.lblTitle.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String listTitle = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.item_list, null);
        }
        TextView tvv = (TextView) convertView.findViewById(R.id.items_list);
        tvv.setText(listTitle);
        tvv.setTypeface(null, Typeface.BOLD);
        CheckBox cb = (CheckBox) convertView.findViewById(R.id.listCheckbox);
        if (itemcheckedTrue) {
            cb.setEnabled(true);
        }

        return convertView;
    }

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

    @Override
    public void itemclicked(int position, boolean status) {
        itemcheckedTrue = status;
        pos = position;
    }

    class ViewHolder {
        TextView lblTitle;
        CheckBox checkedItem;

    }

}


interface GroupItemClicked {
    void itemclicked(int position, boolean status);
}
Terril Thomas
  • 1,486
  • 13
  • 32
  • For reference check this link : http://stackoverflow.com/q/17372750/1562755 – Terril Thomas Jul 25 '16 at 07:19
  • what is case1 and case2 – A.asha Jul 25 '16 at 08:08
  • sir my requirement is when i am click on parent item group check box it will open list of child items and checked items also selected – A.asha Jul 25 '16 at 08:11
  • Case 1 and case 2 are conditions when they are checked and unchecked – Terril Thomas Jul 25 '16 at 08:28
  • ok sir but my requirement is different then what is the procedure – A.asha Jul 25 '16 at 09:05
  • @A.asha : So what u need is when the GroupView is clicked the childview data should be viewable .. correct me if I am wrong – Terril Thomas Jul 25 '16 at 09:23
  • when groupview ccheckbox is checkked then child items is checkedwith respect to parent checkbox – A.asha Jul 25 '16 at 09:35
  • that is my requirement – A.asha Jul 25 '16 at 09:51
  • @A.asha : I have modified the code to showcase you the logic . Rest you can modify the logic more based on your requirement. If you think it's around your required answer please accept the answer. – Terril Thomas Jul 25 '16 at 10:45
  • Just don't copy paste the code . Add your logic as to what you want to when the check box is clicked. Check in this method if you get anything when the groupview is clicked @Override public void itemclicked(int position, boolean status) { itemcheckedTrue = status; pos = position; } – Terril Thomas Jul 25 '16 at 11:26
  • when the parent checkbox is clicked at that same time child checkboxes also checked – A.asha Jul 25 '16 at 11:30
  • So what is the problem .. Did u debug the callback .. and based on the received callback did you refresh the expandablelistview – Terril Thomas Jul 25 '16 at 11:39