Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中listview中gridview的复选框_Android_Listview_Gridview_Checkbox - Fatal编程技术网

Android中listview中gridview的复选框

Android中listview中gridview的复选框,android,listview,gridview,checkbox,Android,Listview,Gridview,Checkbox,我的预订应用程序中有一个问题声明,用户可以删除特定日期或所有日期的所有预订,请参见此图: 我曾尝试使用listview和内部gridview的多个适配器向网格视图添加数据,但这需要数据复制。我怎样才能解决这个问题 每个日期用复选框表示listview,UH、U1等表示gridview的项。我要通过选定的场地id,即,呃,U!列表视图中的etc及其相应日期请尝试以下示例:) MainActivity.java: public class MainActivity extends Activity

我的预订应用程序中有一个问题声明,用户可以删除特定日期或所有日期的所有预订,请参见此图:

我曾尝试使用listview和内部gridview的多个适配器向网格视图添加数据,但这需要数据复制。我怎样才能解决这个问题

每个日期用复选框表示listview,UH、U1等表示gridview的项。我要通过选定的场地id,即,呃,U!列表视图中的etc及其相应日期

请尝试以下示例:)

MainActivity.java:

public class MainActivity extends Activity {

Button clearChecks;
ExpandableListView expandableListView;
ExpandableListGridAdapter expandableListAdapter;
int lastExpandedPosition = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    expandableListView = findViewById(R.id.expandedListView);
    clearChecks = findViewById(R.id.btnClearChecks);

    List<String> listTitle = genGroupList();
    expandableListAdapter = new ExpandableListGridAdapter(this, listTitle, genChildList(listTitle));
    expandableListView.setAdapter(expandableListAdapter);

    expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });
    clearChecks.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            expandableListAdapter.clearChecks();
        }
    });
}

private List<String> genGroupList(){
    List<String> listGroup = new ArrayList<>();
    for(int i=1; i<10; i++){
        listGroup.add("Group: " + i);
    }
    return listGroup;
}

private Map<String, List<ChildItemSample>> genChildList(List<String> header){
    Map<String, List<ChildItemSample>> listChild = new HashMap<>();
    for(int i=0; i<header.size(); i++){
        List<ChildItemSample> testDataList = new ArrayList<>();
        int a = (int)(Math.random() * 8);
        for(int j=0; j<a; j++){
            ChildItemSample testItem = new ChildItemSample("Child " + (j + 1));
            testDataList.add(testItem);
        }
        listChild.put(header.get(i), testDataList);
    }
    return  listChild;
}

}
ExpandableListGridAdapter.java:

public class ExpandableListGridAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
private int checkedBoxesCount;
private boolean[] checkedGroup;

public ExpandableListGridAdapter(Context context, List<String> listGroup, Map<String,
        List<ChildItemSample>> listChild) {
    this.context = context;
    this.listGroup = listGroup;
    this.listChild = listChild;
    checkedBoxesCount = 0;
    checkedGroup = new boolean[listGroup.size()];
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public String getGroup(int groupPosition) {
    return listGroup.get(groupPosition);
}

@Override
public ChildItemSample getChild(int groupPosition, int childPosition) {
    return listChild.get(listGroup.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 b, View view, ViewGroup viewGroup) {
    String itemGroup = getGroup(groupPosition);
    GroupViewHolder groupViewHolder;
    if(view == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.expanded_list_group, null);
        groupViewHolder = new GroupViewHolder();
        groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
        groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
        groupViewHolder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (int)view.getTag();
                checkedGroup[pos] = !checkedGroup[pos];
                for(ChildItemSample item : listChild.get(listGroup.get(pos))){
                    item.setChecked(checkedGroup[pos]);
                }
                notifyDataSetChanged();
            }
        });
        view.setTag(groupViewHolder);
    }else {
        groupViewHolder = (GroupViewHolder)view.getTag();
    }
    groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, listChild.get(listGroup.get(groupPosition)).size()));
    if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
    else groupViewHolder.cbGroup.setChecked(false);
    groupViewHolder.cbGroup.setTag(groupPosition);
    return view;
}

@Override
public View getChildView(final int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_grid_item, null);
    GridLayout childLayout = view.findViewById(R.id.layout_child);

    for(int i = 0; i < listChild.get(listGroup.get(groupPosition)).size(); i++){
        ChildItemSample expandedListText = getChild(groupPosition, i);
        CheckBox cbChild = new CheckBox(context);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = (int)(80 * context.getResources().getDisplayMetrics().density);
        cbChild.setLayoutParams(params);
        cbChild.setChecked(expandedListText.isChecked());
        cbChild.setText(expandedListText.getName());
        cbChild.setTag(i);
        childLayout.addView(cbChild);

        cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb = (CheckBox) view;
                int pos = (int) view.getTag();;
                ChildItemSample selectedItem = listChild.get(listGroup.get(groupPosition)).get(pos);
                selectedItem.setChecked(cb.isChecked());
                if(cb.isChecked()){
                    checkedBoxesCount++;
                    Toast.makeText(context,"Checked value is: " +
                                    listChild.get(listGroup.get(groupPosition)).get(pos).getName(),
                            Toast.LENGTH_SHORT).show();
                }else {
                    checkedBoxesCount--;
                    if(checkedBoxesCount == 0){
                        Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
                    }
                }
                notifyDataSetChanged();
            }
        });
    }
    return view;
}

public void clearChecks() {
    for(int i=0; i<checkedGroup.length; i++) checkedGroup[i] = false;
    for(List<ChildItemSample> value : listChild.values()) {
        for (ChildItemSample sample : value) {
            sample.setChecked(false);
        }
    }
    checkedBoxesCount = 0;
    notifyDataSetChanged();
}

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

private class GroupViewHolder {
    CheckBox cbGroup;
    TextView tvGroup;
}

}
公共类ExpandableListGridAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有列表列表组;
私有地图列表儿童;
私人整数检查框;
私有布尔[]checkedGroup;
公共ExpandableListGridAdapter(上下文上下文、列表组、映射listChild){
this.context=上下文;
this.listGroup=listGroup;
this.listChild=listChild;
checkedBoxesCount=0;
checkedGroup=新布尔值[listGroup.size()];
}
@凌驾
public int getGroupCount(){
返回listGroup.size();
}
@凌驾
公共整数getChildrenCount(整数组位置){
返回1;
}
@凌驾
公共字符串getGroup(int-groupPosition){
返回listGroup.get(groupPosition);
}
@凌驾
公共ChildItemSample getChild(int-groupPosition,int-childPosition){
返回listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
@凌驾
公共长getGroupId(int-groupPosition){
返回组位置;
}
@凌驾
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
@凌驾
公共布尔表ID(){
返回false;
}
@凌驾
公共视图getGroupView(int-groupPosition、布尔b、视图视图、视图组视图组){
字符串itemGroup=getGroup(groupPosition);
GroupViewHolder GroupViewHolder;
如果(视图==null){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.expanded_list_group,null);
groupViewHolder=新的groupViewHolder();
groupViewHolder.tvGroup=view.findviewbyd(R.id.tv\u组);
groupViewHolder.cbGroup=view.findViewById(R.id.cb_组);
groupViewHolder.cbGroup.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int pos=(int)view.getTag();
checkedGroup[pos]=!checkedGroup[pos];
对于(ChildItemSample item:listChild.get(listGroup.get(pos))){
item.setChecked(checkedGroup[pos]);
}
notifyDataSetChanged();
}
});
view.setTag(groupViewHolder);
}否则{
groupViewHolder=(groupViewHolder)view.getTag();
}
groupViewHolder.tvGroup.setText(String.format(“%s(%d)”,itemGroup,listChild.get(listGroup.get(groupPosition)).size());
if(checkedGroup[groupPosition])groupViewHolder.cbGroup.setChecked(true);
else groupViewHolder.cbGroup.setChecked(false);
groupViewHolder.cbGroup.setTag(groupPosition);
返回视图;
}
@凌驾
公共视图getChildView(最终int-groupPosition、int-childPosition、布尔b、视图视图、视图组视图组){
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.list\u grid\u item,空);
GridLayout childLayout=view.findViewById(R.id.layout\u child);
for(int i=0;ipublic class ExpandableListGridAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
private int checkedBoxesCount;
private boolean[] checkedGroup;

public ExpandableListGridAdapter(Context context, List<String> listGroup, Map<String,
        List<ChildItemSample>> listChild) {
    this.context = context;
    this.listGroup = listGroup;
    this.listChild = listChild;
    checkedBoxesCount = 0;
    checkedGroup = new boolean[listGroup.size()];
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return 1;
}

@Override
public String getGroup(int groupPosition) {
    return listGroup.get(groupPosition);
}

@Override
public ChildItemSample getChild(int groupPosition, int childPosition) {
    return listChild.get(listGroup.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 b, View view, ViewGroup viewGroup) {
    String itemGroup = getGroup(groupPosition);
    GroupViewHolder groupViewHolder;
    if(view == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.expanded_list_group, null);
        groupViewHolder = new GroupViewHolder();
        groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
        groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
        groupViewHolder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int pos = (int)view.getTag();
                checkedGroup[pos] = !checkedGroup[pos];
                for(ChildItemSample item : listChild.get(listGroup.get(pos))){
                    item.setChecked(checkedGroup[pos]);
                }
                notifyDataSetChanged();
            }
        });
        view.setTag(groupViewHolder);
    }else {
        groupViewHolder = (GroupViewHolder)view.getTag();
    }
    groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, listChild.get(listGroup.get(groupPosition)).size()));
    if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
    else groupViewHolder.cbGroup.setChecked(false);
    groupViewHolder.cbGroup.setTag(groupPosition);
    return view;
}

@Override
public View getChildView(final int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.list_grid_item, null);
    GridLayout childLayout = view.findViewById(R.id.layout_child);

    for(int i = 0; i < listChild.get(listGroup.get(groupPosition)).size(); i++){
        ChildItemSample expandedListText = getChild(groupPosition, i);
        CheckBox cbChild = new CheckBox(context);
        GridLayout.LayoutParams params = new GridLayout.LayoutParams();
        params.width = (int)(80 * context.getResources().getDisplayMetrics().density);
        cbChild.setLayoutParams(params);
        cbChild.setChecked(expandedListText.isChecked());
        cbChild.setText(expandedListText.getName());
        cbChild.setTag(i);
        childLayout.addView(cbChild);

        cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox cb = (CheckBox) view;
                int pos = (int) view.getTag();;
                ChildItemSample selectedItem = listChild.get(listGroup.get(groupPosition)).get(pos);
                selectedItem.setChecked(cb.isChecked());
                if(cb.isChecked()){
                    checkedBoxesCount++;
                    Toast.makeText(context,"Checked value is: " +
                                    listChild.get(listGroup.get(groupPosition)).get(pos).getName(),
                            Toast.LENGTH_SHORT).show();
                }else {
                    checkedBoxesCount--;
                    if(checkedBoxesCount == 0){
                        Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
                    }
                }
                notifyDataSetChanged();
            }
        });
    }
    return view;
}

public void clearChecks() {
    for(int i=0; i<checkedGroup.length; i++) checkedGroup[i] = false;
    for(List<ChildItemSample> value : listChild.values()) {
        for (ChildItemSample sample : value) {
            sample.setChecked(false);
        }
    }
    checkedBoxesCount = 0;
    notifyDataSetChanged();
}

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

private class GroupViewHolder {
    CheckBox cbGroup;
    TextView tvGroup;
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

    <Button
        android:id="@+id/btnClearChecks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Clear Checks" />

    <ExpandableListView
        android:id="@+id/expandedListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ExpandableListView>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" >

<CheckBox
    android:id="@+id/cb_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="40dp"
    android:layout_gravity="center_vertical" />

<TextView
    android:id="@+id/tv_group"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:textSize="30sp" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_child"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="60dp"
android:columnCount="3"
android:orientation="horizontal">

</GridLayout>