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;
}
}
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!
Related
Hi I followed this Example and implemented expandablelist that keeps track of its childitems(check boxes). I just wanted to extend this feature. I want to keep track of checkbox ,spinner and edittext. I did the following changes but got array index error. Someone please help me to do the same.
Update: I made some progress. I preserved checkbox state. Still edittext and spinner pending.
public class ServiceListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private HashMap<ServiceListModel, List<ServiceListModel>> mListDataChild;
private ArrayList<ServiceListModel> mListDataGroup;
// Hashmap for keeping track of our checkbox check states
private HashMap<Integer, List<ServiceListModel>> mChildCheckStates;
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
private ServiceListModel groupService, childService;
#SuppressLint("UseSparseArrays")
public ServiceListAdapter(Context context, ArrayList<ServiceListModel> listDataGroup, HashMap<ServiceListModel, List<ServiceListModel>> listDataChild) {
mContext = context;
mListDataGroup = listDataGroup;
mListDataChild = listDataChild;
// Initialize our hashmap containing our check states here
mChildCheckStates = new HashMap<>();
}
public int getNumberOfCheckedItemsInGroup(int mGroupPosition) {
List<ServiceListModel> services= mChildCheckStates.get(mGroupPosition);
int count = 0;
if (services != null) {
for (int j = 0; j < services.size(); ++j) {
if (services.get(j).isSelected()) count++;
}
}
return count;
}
#Override
public int getGroupCount() {
return mListDataGroup.size();
}
#Override
public ServiceListModel getGroup(int groupPosition) {
return mListDataGroup.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
groupService = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.nav_list_group_header, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupText = convertView.findViewById(R.id.lblListHeader);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.mGroupText.setText(groupService.getMenuName());
final ImageView arrow = convertView.findViewById(R.id.arrow);
if (getGroup(groupPosition).isHasChildren()) {
arrow.setVisibility(View.VISIBLE);
} else arrow.setVisibility(View.GONE);
if (isExpanded) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
arrow.setImageDrawable(mContext.getDrawable(R.drawable.ic_arrow_up));
} else
arrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_up));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
arrow.setImageDrawable(mContext.getDrawable(R.drawable.ic_arrow_down));
} else
arrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_arrow_down));
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return mListDataChild.get(mListDataGroup.get(groupPosition)).size();
}
#Override
public ServiceListModel getChild(int groupPosition, int childPosition) {
return mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ServiceListModel serviceListModel= getChild(groupPosition, childPosition);
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
childService = getChild(mGroupPosition, mChildPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.services_child_item, null);
childViewHolder = new ChildViewHolder();
childViewHolder.mCheckBox = convertView.findViewById(R.id.serviceCheckBox);
childViewHolder.feeET = convertView.findViewById(R.id.feeET);
childViewHolder.daysSpinner = convertView.findViewById(R.id.daysSpinner);
convertView.setTag(R.layout.services_child_item, childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag(R.layout.services_child_item);
}
childViewHolder.mCheckBox.setText(childService.getMenuName());
childViewHolder.mCheckBox.setOnCheckedChangeListener(null);
if (mChildCheckStates.containsKey(mGroupPosition)) {
List<ServiceListModel> services = mChildCheckStates.get(mGroupPosition);
childViewHolder.mCheckBox.setChecked(services.get(mChildPosition).isSelected());
childViewHolder.feeET.setText(services.get(mChildPosition).getFees());
} else {
List<ServiceListModel> serModel=mListDataChild.get(mListDataGroup.get(groupPosition));
mChildCheckStates.put(mGroupPosition, serModel);
childViewHolder.mCheckBox.setChecked(false);
childViewHolder.feeET.setText("");
}
final List<KeyPairBoolData> days = new ArrayList<>();
KeyPairBoolData h = new KeyPairBoolData();
h.setId(1);
h.setName("1-3 Days");
h.setSelected(false);
days.add(h);
h = new KeyPairBoolData();
h.setId(2);
h.setName("3-7 Days");
h.setSelected(false);
days.add(h);
h = new KeyPairBoolData();
h.setId(3);
h.setName("7-15 Days");
h.setSelected(false);
days.add(h);
h = new KeyPairBoolData();
h.setId(3);
h.setName("Above 15 Days");
h.setSelected(false);
days.add(h);
childViewHolder.daysSpinner.setItems(days, -1, new SpinnerListener() {
#Override
public void onItemsSelected(List<KeyPairBoolData> items) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).isSelected()) {
Log.i("blb07", i + " : " + items.get(i).getName() + " : " + items.get(i).isSelected());
}
}
}
});
childViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
List<ServiceListModel> serv= mChildCheckStates.get(mGroupPosition);
serv.get(mChildPosition).setSelected(isChecked);
mChildCheckStates.put(mGroupPosition, serv);
}
});
childViewHolder.feeET.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.i("blb07","before "+s);
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i("blb07","On "+s);
}
#Override
public void afterTextChanged(Editable s) {
Log.i("blb07","after "+s);
List<ServiceListModel> serv= mChildCheckStates.get(mGroupPosition);
serv.get(mChildPosition).setFees(s.toString());
mChildCheckStates.put(mGroupPosition, serv);
}
});
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
#Override
public boolean hasStableIds() {
return false;
}
public final class GroupViewHolder {
TextView mGroupText;
}
public final class ChildViewHolder {
SingleSpinner daysSpinner;
CheckBox mCheckBox;
EditText feeET;
}
}
The model class
public class ServiceListModel {
public boolean hasChildren, isGroup;
private String menuName, fees, days;
private int id;
private boolean selected;
public ServiceListModel(int id, String menuName, boolean isGroup, boolean hasChildren) {
this.menuName = menuName;
this.isGroup = isGroup;
this.hasChildren = hasChildren;
this.id = id;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMenuName() {
return menuName;
}
public void setMenuName(String menuName) {
this.menuName = menuName;
}
public String getFees() {
return fees;
}
public void setFees(String fees) {
this.fees = fees;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
}
I have not tested it but the problem could be in your loop using preincrement instead of postincrement:
public int getNumberOfCheckedItemsInGroup(int mGroupPosition) {
List<ServiceListModel> services= mChildCheckStates.get(mGroupPosition);
int count = 0;
if (services != null) {
for (int j = 0; j < services.size(); ++j) {
if (services.get(j).isSelected()) count++;
}
}
return count;
}
I guess for should be for (int j = 0; j < services.size(); j++)
I have a check list (Customized expandable list view) that I am populating through adapter. I want to retrieve an array from adapter to Activity on click of a text view in activity.
Activity is as follows, I want to retrieve data in onOptionsItemSelected function:
public class MyPreferencesActivity extends BaseActivity implements PreferencesContract.View, AdapterView.OnItemClickListener, ExpandableListView.OnGroupClickListener {
private PreferencesContract.Presenter mPresenterPreferences;
private Context context;
private ExpandableListView elvListView;
private ArrayList<CategoryModel> categories;
private ExpandableListAdapter ela;
private List<CategoryModel> mCategories;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_preferences);
this.context = this;
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
setupActionbar(toolbar, getString(R.string.preferneces), true, R.drawable.ab_home);
new PreferencesPresenter(this,
Injection.provideUseCaseHandler(),
Injection.provideGetCategories(getApplicationContext())
);
mPresenterPreferences.getCategories();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_save_my_preferences, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
//listener for home
switch (menuItem.getItemId()) {
case R.id.menu_create_event:
break;
}
return super.onOptionsItemSelected(menuItem);
}
private void populateList() {
categories = new ArrayList<>();
for (int i = 0; i < mCategories.size(); i ++){
categories.add(new CategoryModel(mCategories.get(i).getCategoryName(), mCategories.get(i).getCategoryId(), mCategories.get(i).getSubCategories()));
}
}
#Override
public void onItemClick(AdapterView<?> adapterView, android.view.View view, int i, long l) {
}
#Override
public boolean onGroupClick(ExpandableListView expandableListView, android.view.View view, int i, long l) {
return false;
}
#Override
public void setLoading(boolean isActive) {}
public void initViews(){
ela = new ExpandableListAdapter(this, categories);
elvListView = (ExpandableListView) findViewById(R.id.lv_preferences);
elvListView.setAdapter(ela);
elvListView.setOnItemClickListener(this);
elvListView.setOnGroupClickListener(this);
}
#Override
public void showGetPreferencesSuccess(List<CategoryModel> categories) {
setLoading(false);
mCategories = categories;
populateList();
initViews();
}
#Override
public void showGetPreferencesFail(int errorCode) {
}
#Override
public void setPresenter(PreferencesContract.Presenter presenter) {
mPresenterPreferences = Preconditions.checkNotNull(presenter);
}
}
Adapter is as follows:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> item;
private ArrayList<Boolean> itemsChecked;
private ArrayList<ArrayList<Boolean>> childChecked;
private ArrayList<ArrayList<String>> childList;
public ExpandableListAdapter(Context c, ArrayList<CategoryModel> d) {
this.context = c;
this.item = new ArrayList<>();
for (int i = 0; i< d.size(); i++){
this.item.add(d.get(i).getCategoryName());
ArrayList<String> subCat = new ArrayList<>();
for (int j = 0 ; j < d.get(j).getSubCategories().size() ; j++){
subCat.add(d.get(j).getSubCategories().get(j).getSubCategoryName());
}
childList.add(subCat);
}
itemsChecked = new ArrayList<>();
childChecked = new ArrayList<ArrayList<Boolean>>();
for (int i = 0; i < this.item.size() ; i++) {
itemsChecked.add(false);
ArrayList <Boolean>temp = new ArrayList<>();
for (int j = 0 ; j < childList.get(i).size(); j ++){
temp.add(false);
}
childChecked.add(temp);
}
}
#Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.my_preferences_list_group, null);
TextView tvName = (TextView) convertView.findViewById(R.id.tvGroupName);
final CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
if((getChildrenCount(groupPosition) == 0 && itemsChecked.get(groupPosition))|| (getChildrenCount(groupPosition) > 0 && !(childChecked.get(groupPosition).contains(false)))) {
checkBox.setChecked(true);
}
else {
checkBox.setChecked(false);
}
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox)view;
RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
TextView child = (TextView)vwParentRow.getChildAt(0);
final int position = item.indexOf(child.getText());
if (checkBox.isChecked()){
itemsChecked.set(position, true);
for (int i = 0 ; i < getChildrenCount(groupPosition); i++){
childChecked.get(groupPosition).set(i, true);
}
}
else {
itemsChecked.set(position, false);
for (int i = 0 ; i < getChildrenCount(groupPosition); i++){
childChecked.get(groupPosition).set(i, false);
}
}
ExpandableListAdapter.this.notifyDataSetChanged();
}
});
TextView tvCount = (TextView) convertView.findViewById(R.id.tv_item_count);
tvName.setText(item.get(groupPosition));
int childCount = getChildrenCount(groupPosition);
if (childCount > 0) {
tvCount.setText(getChildrenCount(groupPosition) + "items");
tvCount.setVisibility(View.VISIBLE);
}
if (isExpanded) {
}
else {
}
return convertView;
}
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.my_preferences_list_item, null);
TextView tvName = (TextView) convertView.findViewById(R.id.tv_item);
CheckBox checkBox = (CheckBox)convertView.findViewById(R.id.checkBox);
if (childChecked.get(groupPosition).get(childPosition))
checkBox.setChecked(true);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox)view;
RelativeLayout vwParentRow = (RelativeLayout)view.getParent();
TextView child = (TextView)vwParentRow.getChildAt(0);
int position = -1;
for (int i = 0 ; i < childList.size(); i++){
if (childList.get(i).contains(child.getText())){
if(checkBox.isChecked()) {
childChecked.get(i).set(childList.get(i).indexOf(child.getText()) , true);
ExpandableListAdapter.this.notifyDataSetChanged();
}
else {
childChecked.get(i).set(childList.get(i).indexOf(child.getText()) , false);
ExpandableListAdapter.this.notifyDataSetChanged();
}
}
}
if (checkBox.isChecked()){
if (position >0 )
itemsChecked.set(position, true);
}
else if(position > 0) {
itemsChecked.set(position, false);
}
}
});
String s = childList.get(groupPosition).get(childPosition);
tvName.setText(s);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
}
#Override
public int getGroupCount() {
return item.size();
}
#Override
public Object getGroup(int groupPosition) {
return item.get(groupPosition);
}
#Override
public long getGroupId(int groupPosition) {
return 0;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
I have an idea of sending data from Activity to Adapter.
Any help in doing it some other way?
I think it's as simple as creating a getter inside your adapter
public List<String> getItems() {
return this.item;
}
And inside onOptionsItemSelected in the activity
ela.getItems();
public class MainActivity extends AppCompatActivity {
private CollapsingToolbarLayout collapsingToolbarLayout = null;
HashMap<String, List<ExpandableListItem>> _child;
String response;
String serviceName, serviceIcon, serviceId;
ExpandableListView expandableListView;
CustomExpandableListAdapter expandableListAdapter;
String groupName;
String childName;
List<ExpandableListItem> listtG1;
JSONArray jsonArray;
List<ExpandableListItem> _header;
ExpandableListItem rowData;
List<String> _groupNameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle(getResources().getString(R.string.user_name));
new CategoryServices().execute("");
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
groupName = _groupNameList.get(groupPosition);
listtG1 = _child.get(groupName);
Log.d("ListSize", String.valueOf(listtG1.size()));
Toast.makeText(MainActivity.this,groupName+""+childName,Toast.LENGTH_LONG).show();
}
});
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(MainActivity.this,groupName+""+childName,Toast.LENGTH_LONG).show();
}
});
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
groupName = _groupNameList.get(groupPosition);
listtG1 = _child.get(groupName);
Toast.makeText(MainActivity.this, groupName,Toast.LENGTH_LONG).show();
Log.d("ListSize", String.valueOf(listtG1.size()));
if (listtG1.size() == 1 && listtG1.get(0).getServiceName().equals(groupName)) {
expandableListView.setGroupIndicator(null);
return true;
}
return false;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
childName = listtG1.get(childPosition).getServiceName();
Toast.makeText(
getApplicationContext(),
childName, Toast.LENGTH_SHORT
).show();
return false;
}
});
dynamicToolbarColor();
toolbarTextAppernce();
}
private void dynamicToolbarColor() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.profile_pic);
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
#Override
public void onGenerated(Palette palette) {
collapsingToolbarLayout.setContentScrimColor(palette.getMutedColor(R.attr.colorPrimary));
collapsingToolbarLayout.setStatusBarScrimColor(palette.getMutedColor(R.attr.colorPrimaryDark));
}
});
}
private void toolbarTextAppernce() {
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.expandedappbar);
}
class CategoryServices extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... params) {
response = JSONFunctions.getJSONfromURL("http://cpanel.smartindiaservice.com/api/categoryservices?categoryid=13");
return response;
}
#Override
protected void onPostExecute(String result) {
try {
setListAdapter(result, expandableListView);
// return false;
} catch (NullPointerException e) {
}
super.onPostExecute(result);
}
}
private void setListAdapter(String response, ExpandableListView expandableListView) {
_child = new HashMap<>();
_groupNameList = new ArrayList<>();
if (!response.isEmpty()) {
try {
jsonArray = new JSONArray(response);
if (jsonArray != null && jsonArray.length() > 0) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject productObj = jsonArray.getJSONObject(i);
String groupName = productObj.getString("Service");
JSONArray servicesArray = productObj.getJSONArray("SubServices");
if (servicesArray != null && servicesArray.length() > 0) {
_header = new ArrayList<>();
for (int j = 0; j < servicesArray.length(); j++) {
JSONObject serviceObj = servicesArray.getJSONObject(j);
rowData = new ExpandableListItem();
serviceName = serviceObj.getString("SubServiceName");
serviceIcon = serviceObj.getString("SubServiceDescription");
serviceId = serviceObj.getString("SubServiceID");
rowData.setGroupName(groupName);
rowData.setServiceIcon(serviceIcon);
rowData.setServiceId(serviceId);
rowData.setServiceName(serviceName);
_header.add(rowData);
}
}
_child.put(groupName, _header);
_groupNameList.add(groupName);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
expandableListAdapter = new CustomExpandableListAdapter(this, _groupNameList, _child);
expandableListView.setAdapter(expandableListAdapter);
}
}
}
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<ExpandableListItem>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<ExpandableListItem>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// final String expandedListText = getChild(listPosition, expandedListPosition).g;
ExpandableListItem rowData = (ExpandableListItem) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(rowData.getServiceName());
return convertView;
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
this is my activity and adapter i am able to display data in expandable list-view i want show indicator when group contain more than one child and i want hide group indicator when group contain only one child and group name and child name is same i am trying to in activity expandableListView.setGroupIndicator(null); using this code on setOnGroupClickListener this click but what happed firs time it display all indicator and when i click on group whose child is one and name same then it hide all indicator please suggest me how to fix this issue
[Adapter class]
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<FilterList>> expandableListDetail;
private boolean checked;
private int lastClickedPosition;
private List<Boolean> setValueForSeletedFilter;
private String[] keysOfHashmap;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<FilterList>> expandableListDetail, List<Boolean> setValueForSeletedFilter) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
this.setValueForSeletedFilter = setValueForSeletedFilter;
}
#Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition).getSearchFilterValue();
}
#Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
#Override
public View getChildView(final int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
int i = listPosition;
int j = expandedListPosition;
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
TextView expandedListTextView = (TextView) convertView
.findViewById(R.id.expandedListItem);
expandedListTextView.setText(expandedListText);
String s = expandableListTitle.get(listPosition);
final ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_select);
if (setValueForSeletedFilter.get(expandedListPosition) == true) {
imageView.setVisibility(View.VISIBLE);
} else {
imageView.setVisibility(View.GONE);
}
}
return convertView;
}
private void toggleSelection(int i, View v) {
int j = i;
ImageView imageView = (ImageView) v.findViewById(R.id.iv_select);
imageView.setImageResource(R.drawable.ic_review);
}
#Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
#Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
#Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
#Override
public long getGroupId(int listPosition) {
return listPosition;
}
#Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.listTitle);
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
public void setData(){
notifyDataSetChanged();
}
#Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
[setting values for adapter class and notifying adapter on changing values]
final HashMap> setValueForSeletedFilter = new HashMap<>();
String header = null;
final List booleanList = new ArrayList<>();
final HashMap> stringListHashMap = new HashMap<>();
final List expandableListTitle;
for (int i = 0; i < filtersInfos.size(); i++) {
stringListHashMap.put(filtersInfos.get(i).getFilterHeaderName(), filtersInfos.get(i).getFilterLists());
if (filtersInfos.get(i).getFilterHeaderName().equalsIgnoreCase("Distance")) {
header = filtersInfos.get(i).getFilterHeaderName();
for (int j = 0; j < filtersInfos.get(i).getFilterLists().size(); j++)
booleanList.add(false);
}
}
setValueForSeletedFilter.put(header, booleanList);
expandableListTitle = new ArrayList<String>(stringListHashMap.keySet());
final CustomExpandableListAdapter adapter = new CustomExpandableListAdapter(this, expandableListTitle, stringListHashMap, booleanList);
expandableListView.setAdapter(adapter);
filterDialog.show();
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Expanded.",
Toast.LENGTH_SHORT).show();
}
});
expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
expandableListTitle.get(groupPosition) + " List Collapsed.",
Toast.LENGTH_SHORT).show();
}
});
final boolean[] checked = {false};
final int[] lastClickedPosition = {0};
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (expandableListTitle.get(groupPosition).equalsIgnoreCase("Distance")) {
booleanList.add(childPosition, true);
setValueForSeletedFilter.put(expandableListTitle.get(groupPosition), booleanList);
expandableListView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
return false;
}
});
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
filterDialog.dismiss();
}
});
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
on the basis of header make list single or multi selection android.
getChildView() example for my case "Single selection"
#Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
View v = LayoutInflater.from(context).inflate(R.layout.expandablelv_child, null);
final CheckBox checkBox = v.findViewById(R.id.expandable_lv_child_cb);
RelativeLayout quantityContainer = v.findViewById(R.id.expandable_lv_child_quantity_cont);
final TextView textView = v.findViewById(R.id.expandable_lv_child_quantity_tv);
checkBox.setText(children.get(i).get(i1).getItemName());
double d=Double.parseDouble(children.get(i).get(i1).getMinQty());
int k=(int)d;
textView.setText(String.valueOf(k));
quantityContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
QuantityDialog dialog = new QuantityDialog(context, children.get(i).get(i1).getMinQty(),
children.get(i).get(i1).getMaxQty(), new DialogSelectable() {
#Override
public void onQuantitySelected(String quantity) {
if (group[i].parent == 1 && group[i].child == i1)
textView.setText(quantity);
ExpandableLvAdapter.quantity[i] = quantity;
}
});
dialog.show();
}
});
try {
// if (model.children.get(i).size() < children.get(i).size()) {
try {
model.children.get(i).add(checkBox);
} catch (Exception e) {
ArrayList<CheckBox> temp = new ArrayList<>();
temp.add(checkBox);
model.children.add(temp);
}
//}
} catch (Exception e) {
try {
model.children.get(i).add(checkBox);
} catch (Exception ex) {
ArrayList<CheckBox> temp = new ArrayList<>();
temp.add(checkBox);
model.children.add(temp);
}
}
if (group[i].parent == 1) {
model.children.get(i).get(group[i].child).setEnabled(true);
model.children.get(i).get(group[i].child).setChecked(true);
model.disable(i, children.get(i).get(group[i].child).getItemName());
} else {
model.enable(i);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
group[i].parent = 1;
group[i].child = i1;
model.disable(i, children.get(i).get(i1).getItemName());
selected.add(children.get(i).get(i1));
quantity[i] = children.get(i).get(i1).getMaxQty();
} else {
group[i].parent = 0;
model.enable(i);
}
}
});
return v;
}
Hey people I'm Implementing TreeView using ExpandableListView. But I have some measure problem that I unfortunately can't solve it.
Here's ScreenShots of the problem:
You can see that there are problems in measuring. As long as I'm new to Android I don't really understand onMeasure() method.
I have 1 ExpandableListView and in it's getChildView() i return CustomExapndableListView-s.
Here's code:
ExpandableListAdapter :
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listDataChild;
public ExpandableListAdapter (Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listDataChild = listChildData;
}
#Override
public int getGroupCount() {
return this.listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return this.listDataChild.get(this.listDataHeader.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return this.listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.listDataChild.get(this.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) {
String headerTitle = (String) getGroup(groupPosition);
Log.i("Header: ", " " + headerTitle);
if(convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
/*final String childText = (String) getChild(groupPosition, childPosition);
if(convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);*/
String childText = (String) getChild(groupPosition, childPosition);
if(listDataChild.containsKey(childText)){
Log.i("Child", "" + childText);
CustomExpandableListView explv = new CustomExpandableListView(context);
// explv.setRows(calculateRowCount((String)getGroup(groupPosition), null));
// ChildLayerExpandableListAdapter adapter = new ChildLayerExpandableListAdapter(context, listDataChild.get(getGroup(groupPosition)), listDataChild);
Log.i("Opaaaa:", " " + getGroup(groupPosition));
List<String> newHeaders = new ArrayList <String>();
newHeaders.add(childText);
// listDataChild.get(getGroup(groupPosition))
ExpandableListAdapter adapter = new ExpandableListAdapter(context, newHeaders, listDataChild);
explv.setAdapter(adapter);
explv.setGroupIndicator(null);
convertView = explv;
convertView.setPadding(20, 0, 0, 0);
}else{
// if(convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
// }
Log.i("Else:", " " + childText);
TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
convertView.setPadding(20, 0, 0, 0);
}
return convertView;
}
private int calculateRowCount (String key, ExpandableListView listView) {
int groupCount = listDataChild.get(key).size();
int rowCtr = 0;
for(int i = 0; i < groupCount; i++) {
rowCtr++;
if( (listView != null) && (listView.isGroupExpanded(i)))
rowCtr += listDataChild.get(listDataChild.get(key).get(i)).size() - 1;
}
return rowCtr;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
CustomExpandableListView :
public class CustomExpandableListView extends ExpandableListView {
private static final int HEIGHT = 20;
private int rows;
public void setRows(int rows) {
this.rows = rows;
}
public CustomExpandableListView(Context context) {
super(context);
}
public CustomExpandableListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(500, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// setMeasuredDimension(getMeasuredWidth(), rows * HEIGHT);
}
}
What I want to do is,when I expand the view, I want all children to be shown and I could just scroll down the ListView. Right now some children are hidden under different children.
Thanks in Advance for your Help.
#Code Word I'm sorry, I forgot to share my solution.
As I mentioned before I implemented treeView with ListView. Here is my source code.
TreeElementI.java :
public interface TreeElementI extends Serializable{
public void addChild(TreeElementI child);
public String getId();
public void setId(String id);
public String getOutlineTitle();
public void setOutlineTitle(String outlineTitle);
public boolean isHasParent();
public void setHasParent(boolean hasParent);
public boolean isHasChild();
public void setHasChild(boolean hasChild);
public int getLevel();
public void setLevel(int level);
public boolean isExpanded();
public void setExpanded(boolean expanded);
public ArrayList<TreeElementI> getChildList();
public TreeElementI getParent();
public void setParent(TreeElementI parent);
}
TreeElement.java :
public class TreeElement implements TreeElementI{
private String id;
private String outlineTitle;
private boolean hasParent;
private boolean hasChild;
private TreeElementI parent;
private int level;
private ArrayList<TreeElementI> childList;
private boolean expanded;
public TreeElement(String id, String outlineTitle) {
super();
this.childList = new ArrayList<TreeElementI>();
this.id = id;
this.outlineTitle = outlineTitle;
this.level = 0;
this.hasParent = true;
this.hasChild = false;
this.parent = null;
}
public TreeElement(String id, String outlineTitle, boolean hasParent, boolean hasChild, TreeElement parent, int level, boolean expanded) {
super();
this.childList = new ArrayList<TreeElementI>();
this.id = id;
this.outlineTitle = outlineTitle;
this.hasParent = hasParent;
this.hasChild = hasChild;
this.parent = parent;
if(parent != null) {
this.parent.getChildList().add(this);
}
this.level = level;
this.expanded = expanded;
}
#Override
public void addChild(TreeElementI child) {
this.getChildList().add(child);
this.setHasParent(false);
this.setHasChild(true);
child.setParent(this);
child.setLevel(this.getLevel() + 1);
}
#Override
public String getId() {
return this.id;
}
#Override
public void setId(String id) {
this.id = id;
}
#Override
public String getOutlineTitle() {
return this.outlineTitle;
}
#Override
public void setOutlineTitle(String outlineTitle) {
this.outlineTitle = outlineTitle;
}
#Override
public boolean isHasParent() {
return this.hasParent;
}
#Override
public void setHasParent(boolean hasParent) {
this.hasParent = hasParent;
}
#Override
public boolean isHasChild() {
return this.hasChild;
}
#Override
public void setHasChild(boolean hasChild) {
this.hasChild = hasChild;
}
#Override
public int getLevel() {
return this.level;
}
#Override
public void setLevel(int level) {
this.level = level;
}
#Override
public boolean isExpanded() {
return this.expanded;
}
#Override
public void setExpanded(boolean expanded) {
this.expanded = expanded;
}
#Override
public ArrayList<TreeElementI> getChildList() {
return this.childList;
}
#Override
public TreeElementI getParent() {
return this.parent;
}
#Override
public void setParent(TreeElementI parent) {
this.parent = parent;
}
}
TreeViewClassifAdapter.java :
public class TreeViewClassifAdapter extends BaseAdapter {
private static final int TREE_ELEMENT_PADDING_VAL = 25;
private List<TreeElementI> fileList;
private Context context;
private Bitmap iconCollapse;
private Bitmap iconExpand;
private Dialog dialog;
private EditText textLabel;
private XTreeViewClassif treeView;
public TreeViewClassifAdapter(Context context, List<TreeElementI> fileList, Dialog dialog, EditText textLabel, XTreeViewClassif treeView) {
this.context = context;
this.fileList = fileList;
this.dialog = dialog;
this.textLabel = textLabel;
this.treeView = treeView;
iconCollapse = BitmapFactory.decodeResource(context.getResources(), R.drawable.x_treeview_outline_list_collapse);
iconExpand = BitmapFactory.decodeResource(context.getResources(), R.drawable.x_treeview_outline_list_expand);
}
public List<TreeElementI> getListData() {
return this.fileList;
}
#Override
public int getCount() {
return this.fileList.size();
}
#Override
public Object getItem(int position) {
return this.fileList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = View.inflate(context, R.layout.x_treeview_classif_list_item, null);
holder = new ViewHolder();
holder.setTextView((TextView) convertView.findViewById(R.id.text));
holder.setImageView((ImageView) convertView.findViewById(R.id.icon));
convertView.setTag(holder);
final TreeElementI elem = (TreeElementI) getItem(position);
int level = elem.getLevel();
holder.getIcon().setPadding(TREE_ELEMENT_PADDING_VAL * (level + 1), holder.icon.getPaddingTop(), 0, holder.icon.getPaddingBottom());
holder.getText().setText(elem.getOutlineTitle());
if (elem.isHasChild() && (elem.isExpanded() == false)) {
holder.getIcon().setImageBitmap(iconCollapse);
} else if (elem.isHasChild() && (elem.isExpanded() == true)) {
holder.getIcon().setImageBitmap(iconExpand);
} else if (!elem.isHasChild()) {
holder.getIcon().setImageBitmap(iconCollapse);
holder.getIcon().setVisibility(View.INVISIBLE);
}
IconClickListener iconListener = new IconClickListener(this, position);
TextClickListener txtListener = new TextClickListener((ArrayList<TreeElementI>) this.getListData(), position);
holder.getIcon().setOnClickListener(iconListener);
holder.getText().setOnClickListener(txtListener);
return convertView;
}
private class ViewHolder {
ImageView icon;
TextView text;
public TextView getText() {
return this.text;
}
public void setTextView(TextView text) {
this.text = text;
}
public ImageView getIcon() {
return this.icon;
}
public void setImageView(ImageView icon) {
this.icon = icon;
}
}
/**
* Listener For TreeElement Text Click
*/
private class TextClickListener implements View.OnClickListener {
private ArrayList<TreeElementI> list;
private int position;
public TextClickListener(ArrayList<TreeElementI> list, int position) {
this.list = list;
this.position = position;
}
#Override
public void onClick(View v) {
treeView.setXValue(String.valueOf(list.get(position).getId()));
dialog.dismiss();
}
}
/**
* Listener for TreeElement "Expand" button Click
*/
private class IconClickListener implements View.OnClickListener {
private ArrayList<TreeElementI> list;
private TreeViewClassifAdapter adapter;
private int position;
public IconClickListener(TreeViewClassifAdapter adapter, int position) {
this.list = (ArrayList<TreeElementI>) adapter.getListData();
this.adapter = adapter;
this.position = position;
}
#Override
public void onClick(View v) {
if (!list.get(position).isHasChild()) {
return;
}
if (list.get(position).isExpanded()) {
list.get(position).setExpanded(false);
TreeElementI element = list.get(position);
ArrayList<TreeElementI> temp = new ArrayList<TreeElementI>();
for (int i = position + 1; i < list.size(); i++) {
if (element.getLevel() >= list.get(i).getLevel()) {
break;
}
temp.add(list.get(i));
}
list.removeAll(temp);
adapter.notifyDataSetChanged();
} else {
TreeElementI obj = list.get(position);
obj.setExpanded(true);
int level = obj.getLevel();
int nextLevel = level + 1;
for (TreeElementI element : obj.getChildList()) {
element.setLevel(nextLevel);
element.setExpanded(false);
list.add(position + 1, element);
}
adapter.notifyDataSetChanged();
}
}
}
}
I hope it's not too late and this source code will help you.