My requirement is in expandble list 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?
Expndbleadapter 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
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://ift.tt/nIICcg"
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"/>
group_list.xml
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
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"/>
Aucun commentaire:
Enregistrer un commentaire