问题
I have an expandable list View that has a check button, but the problem is I can only add one child item.
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
dataItem = new DataItem();
dataItem.setCategoryName("Art");
arSubCategory = new ArrayList<>();
for(int j = 1; j < 6; j++) {
SubCategoryItem subCategoryItem = newSubCategoryItem();
subCategoryItem.setIsChecked(ConstantManager.CHECK_BOX_CHECKED_FALSE);
subCategoryItem.setSubCategoryName("Art: "+j);
arSubCategory.add(subCategoryItem);
}
I have tried to use but this has no check
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("do this and do that");
listDataHeader.add("do this and don't do that");
listDataHeader.add("don't do that but you can do this");
// Adding child data
List<String> 250 = new ArrayList<String>();
250.add("hmm what is this you doing");
// Header, Child data
listDataChild.put(listDataHeader.get(0), 250);
}
but the thing is that one is HashMap<String, List<String>>()
and the other is ArrayList<ArrayList<HashMap<String, String>>>
so it can work.
Is there a way I can make the first code add more item?
This is the adapter
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.apps.ayodkay.services.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MyCategoriesExpandableListAdapter extends
BaseExpandableListAdapter {
public static ArrayList<ArrayList<HashMap<String, String>>> childItems;
public static ArrayList<HashMap<String, String>> parentItems;
// private final ArrayList<HashMap<String, String>> childItems;
private LayoutInflater inflater;
private Activity activity;
private HashMap<String, String> child;
private int count = 0;
private boolean isFromMyCategoriesFragment;
public MyCategoriesExpandableListAdapter(Activity activity, ArrayList<HashMap<String, String>> parentItems,
ArrayList<ArrayList<HashMap<String, String>>> childItems,boolean isFromMyCategoriesFragment) {
MyCategoriesExpandableListAdapter.parentItems = parentItems;
MyCategoriesExpandableListAdapter.childItems = childItems;
this.activity = activity;
this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getGroupCount() {
return parentItems.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return (childItems.get(groupPosition)).size();
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderParent viewHolderParent;
if (convertView == null) {
if(isFromMyCategoriesFragment) {
convertView = inflater.inflate(R.layout.group_list_layout_my_categories, null);
}else {
convertView = inflater.inflate(R.layout.group_list_layout_choose_categories, null);
}
viewHolderParent = new ViewHolderParent();
viewHolderParent.tvMainCategoryName = convertView.findViewById(R.id.tvMainCategoryName);
viewHolderParent.cbMainCategory = convertView.findViewById(R.id.cbMainCategory);
viewHolderParent.ivCategory = convertView.findViewById(R.id.ivCategory);
convertView.setTag(viewHolderParent);
} else {
viewHolderParent = (ViewHolderParent) convertView.getTag();
}
if (parentItems.get(groupPosition).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
viewHolderParent.cbMainCategory.setChecked(true);
notifyDataSetChanged();
} else {
viewHolderParent.cbMainCategory.setChecked(false);
notifyDataSetChanged();
}
viewHolderParent.cbMainCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolderParent.cbMainCategory.isChecked()) {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
childItems.get(groupPosition).get(i).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
}
notifyDataSetChanged();
}
else {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
childItems.get(groupPosition).get(i).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
}
notifyDataSetChanged();
}
}
});
ConstantManager.childItems = childItems;
ConstantManager.parentItems = parentItems;
viewHolderParent.tvMainCategoryName.setText(parentItems.get(groupPosition).get(ConstantManager.Parameter.CATEGORY_NAME));
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderChild viewHolderChild;
child = childItems.get(groupPosition).get(childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_list_layout_choose_category, null);
viewHolderChild = new ViewHolderChild();
viewHolderChild.tvSubCategoryName = convertView.findViewById(R.id.tvSubCategoryName);
viewHolderChild.cbSubCategory = convertView.findViewById(R.id.cbSubCategory);
convertView.setTag(viewHolderChild);
} else {
viewHolderChild = (ViewHolderChild) convertView.getTag();
}
if (childItems.get(groupPosition).get(childPosition).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
viewHolderChild.cbSubCategory.setChecked(true);
notifyDataSetChanged();
} else {
viewHolderChild.cbSubCategory.setChecked(false);
notifyDataSetChanged();
}
viewHolderChild.tvSubCategoryName.setText(child.get(ConstantManager.Parameter.SUB_CATEGORY_NAME));
viewHolderChild.cbSubCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (viewHolderChild.cbSubCategory.isChecked()) {
count = 0;
childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
notifyDataSetChanged();
} else {
count = 0;
childItems.get(groupPosition).get(childPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
notifyDataSetChanged();
}
for (int i = 0; i < childItems.get(groupPosition).size(); i++) {
if (childItems.get(groupPosition).get(i).get(ConstantManager.Parameter.IS_CHECKED).equalsIgnoreCase(ConstantManager.CHECK_BOX_CHECKED_TRUE)) {
count++;
}
}
if (count == childItems.get(groupPosition).size()) {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_TRUE);
notifyDataSetChanged();
} else {
parentItems.get(groupPosition).put(ConstantManager.Parameter.IS_CHECKED, ConstantManager.CHECK_BOX_CHECKED_FALSE);
notifyDataSetChanged();
}
ConstantManager.childItems = childItems;
ConstantManager.parentItems = parentItems;
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
private class ViewHolderParent {
TextView tvMainCategoryName;
CheckBox cbMainCategory;
ImageView ivCategory;
}
private class ViewHolderChild {
TextView tvSubCategoryName;
CheckBox cbSubCategory;
}
}
回答1:
I think that you have custom classes similar to these:
DataItem:
public class DataItem {
private String categoryName;
private ArrayList<SubCategoryItem> subCategory;
private boolean isChecked;
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public ArrayList<SubCategoryItem> getSubCategory() {
return subCategory;
}
public void setSubCategory(ArrayList<SubCategoryItem> subCategory) {
this.subCategory = subCategory;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public DataItem(){
categoryName = "Default Category";
subCategory = new ArrayList<>();
isChecked = false;
}
}
SubCategoryItem:
public class SubCategoryItem {
private String subCategoryName;
private boolean isChecked;
public String getSubCategoryName() {
return subCategoryName;
}
public void setSubCategoryName(String subCategoryName) {
this.subCategoryName = subCategoryName;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean checked) {
isChecked = checked;
}
public SubCategoryItem(){
subCategoryName = "Default Sub Category";
isChecked = false;
}
}
Then you can use those directly for the adapter, like below:
public class MyCategoriesExpandableListAdapter extends BaseExpandableListAdapter {
public ArrayList<DataItem> dataList;
private LayoutInflater inflater;
private Activity activity;
private int count;
private boolean isFromMyCategoriesFragment;
public MyCategoriesExpandableListAdapter(Activity activity, ArrayList<DataItem> dataList, boolean isFromMyCategoriesFragment) {
this.activity = activity;
this.dataList = dataList;
this.isFromMyCategoriesFragment = isFromMyCategoriesFragment;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getGroupCount() {
return dataList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return dataList.get(groupPosition).getSubCategory().size();
}
@Override
public DataItem getGroup(int i) {
return dataList.get(i);
}
@Override
public SubCategoryItem getChild(int i, int i1) {
return dataList.get(i).getSubCategory().get(i1);
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int groupPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderParent viewHolderParent;
if (convertView == null) {
if(isFromMyCategoriesFragment) {
convertView = inflater.inflate(R.layout.group_list_layout_my_categories, null);
}else {
convertView = inflater.inflate(R.layout.group_list_layout_choose_categories, null);
}
viewHolderParent = new ViewHolderParent();
viewHolderParent.tvMainCategoryName = convertView.findViewById(R.id.tvMainCategoryName);
viewHolderParent.cbMainCategory = convertView.findViewById(R.id.cbMainCategory);
viewHolderParent.ivCategory = convertView.findViewById(R.id.ivCategory);
convertView.setTag(viewHolderParent);
} else {
viewHolderParent = (ViewHolderParent) convertView.getTag();
}
viewHolderParent.cbMainCategory.setChecked(dataList.get(groupPosition).isChecked());
viewHolderParent.cbMainCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dataList.get(groupPosition).setChecked(viewHolderParent.cbMainCategory.isChecked());
for (int i = 0; i < dataList.get(groupPosition).getSubCategory().size(); i++) {
dataList.get(groupPosition).getSubCategory().get(i).setChecked(viewHolderParent.cbMainCategory.isChecked());
}
notifyDataSetChanged();
}
});
//ConstantManager.childItems = childItems;
//ConstantManager.parentItems = parentItems;
viewHolderParent.tvMainCategoryName.setText(dataList.get(groupPosition).getCategoryName());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean b, View convertView, ViewGroup viewGroup) {
final ViewHolderChild viewHolderChild;
SubCategoryItem child = getChild(groupPosition, childPosition);
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_list_layout_choose_category, null);
viewHolderChild = new ViewHolderChild();
viewHolderChild.tvSubCategoryName = convertView.findViewById(R.id.tvSubCategoryName);
viewHolderChild.cbSubCategory = convertView.findViewById(R.id.cbSubCategory);
convertView.setTag(viewHolderChild);
} else {
viewHolderChild = (ViewHolderChild) convertView.getTag();
}
viewHolderChild.cbSubCategory.setChecked(child.isChecked());
viewHolderChild.tvSubCategoryName.setText(child.getSubCategoryName());
viewHolderChild.cbSubCategory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getChild(groupPosition, childPosition).setChecked(viewHolderChild.cbSubCategory.isChecked());
count = 0;
for (int i = 0; i < getGroup(groupPosition).getSubCategory().size(); i++) {
if (getChild(groupPosition, i).isChecked()) {
count++;
}
}
getGroup(groupPosition).setChecked(count == getGroup(groupPosition).getSubCategory().size());
notifyDataSetChanged();
//ConstantManager.childItems = childItems;
//ConstantManager.parentItems = parentItems;
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
private class ViewHolderParent {
TextView tvMainCategoryName;
CheckBox cbMainCategory;
ImageView ivCategory;
}
private class ViewHolderChild {
TextView tvSubCategoryName;
CheckBox cbSubCategory;
}
public ArrayList<SubCategoryItem> getCheckedList() {
ArrayList<SubCategoryItem> checkedItemList = new ArrayList<>();
for (DataItem dataItem : dataList) {
for (SubCategoryItem subCategoryItem : dataItem.getSubCategory()) {
if(subCategoryItem.isChecked()) checkedItemList.add(subCategoryItem);
}
}
return checkedItemList;
}
}
Create the data list like this:
String[] sampleGroups = {"Art", "Maths", "Language"};
ArrayList<DataItem> arCategory = new ArrayList<>();
for(int i = 0; i < sampleGroups.length; i++){
DataItem dataItem = new DataItem();
dataItem.setCategoryName(sampleGroups[i]);
ArrayList<SubCategoryItem> arSubCategory = new ArrayList<>();
for(int j = 1; j < 6; j++) {
SubCategoryItem subCategoryItem = new SubCategoryItem();
subCategoryItem.setSubCategoryName(dataItem.getCategoryName() + ": " + j);
arSubCategory.add(subCategoryItem);
}
dataItem.setSubCategory(arSubCategory);
arCategory.add(dataItem);
}
Hope that helps!
来源:https://stackoverflow.com/questions/54577619/adding-different-options-for-expandable-listview