android getChildView is not being called after notifyDataSetChanged - android

I have a expandableListView and tried to change its childs like that:
((SubItem) adapter.getChild(i+1, 5)).setCount(unreadBox);
adapter.notifyDataSetChanged();
Here is my adapter class:
public class DrawerAdapter extends BaseExpandableListAdapter {
private Context context;
private List<Item> items;
private List<List<SubItem>> subItems;
public DrawerAdapter(Context context, List<Item> items, List<List<SubItem>> subItems) {
this.context = context;
this.items = items;
this.subItems = subItems;
}
#Override
public int getGroupCount() {
return items.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return subItems.get(groupPosition).size();
}
#Override
public Object getGroup(int position) {
return items.get(position);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return subItems.get(groupPosition).get(childPosition);
}
#Override
public long getGroupId(int position) {
return position;
}
#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 view, ViewGroup viewGroup) {
Item item = (Item) getGroup(groupPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_drawer_group, null);
}
ImageView iconView = (ImageView) view.findViewById(R.id.icon_view);
if (item.iconDrawableId != null) {
iconView.setImageResource(item.iconDrawableId);
iconView.setVisibility(View.VISIBLE);
} else {
iconView.setVisibility(View.GONE);
}
TextView groupHeader = (TextView) view.findViewById(R.id.group_header);
groupHeader.setText(item.title);
if (item.iconDrawableId != null) {
groupHeader.setPadding(
DimensionHelper.dpToPixels(context, 4.0f), 0,
DimensionHelper.dpToPixels(context, 16.0f), 0);
} else {
groupHeader.setPadding(
DimensionHelper.dpToPixels(context, 16.0f), 0,
DimensionHelper.dpToPixels(context, 16.0f), 0);
}
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
imageView.setImageResource(isExpanded ? R.drawable.ic_navigation_collapse : R.drawable.ic_navigation_expand);
imageView.setVisibility(getChildrenCount(groupPosition) > 0 ? View.VISIBLE : View.GONE);
return view;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isExpanded, View view, ViewGroup viewGroup) {
SubItem subItem = (SubItem) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_drawer_item, null);
}
TextView colorPreView = (TextView) view.findViewById(R.id.colorPreView);
if (subItem.getColor() != -1) {
colorPreView.setBackgroundColor(subItem.getColor());
colorPreView.setVisibility(View.VISIBLE);
} else {
colorPreView.setVisibility(View.GONE);
}
ImageView iconView = (ImageView) view.findViewById(R.id.icon_view);
if (subItem.iconDrawableId != null) {
iconView.setImageResource(subItem.iconDrawableId);
iconView.setVisibility(View.VISIBLE);
} else {
iconView.setVisibility(View.GONE);
}
TextView title = (TextView) view.findViewById(R.id.title_text_view);
title.setText(subItem.title);
if (subItem.iconDrawableId != null) {
title.setPadding(
DimensionHelper.dpToPixels(context, 4.0f), 0,
DimensionHelper.dpToPixels(context, 16.0f), 0);
} else {
title.setPadding(
DimensionHelper.dpToPixels(context, 40.0f), 0,
DimensionHelper.dpToPixels(context, 16.0f), 0);
}
TextView counter = (TextView) view.findViewById(R.id.counter_text_view);
counter.setText(String.valueOf(subItem.count));
counter.setVisibility(subItem.count > 0 ? View.VISIBLE : View.GONE);
return view;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public static class Item {
protected String title;
protected Integer iconDrawableId;
protected Listener listener;
public Item(String title, Integer iconDrawableId, Listener listener) {
this.title = title;
this.iconDrawableId = iconDrawableId;
this.listener = listener;
}
public Listener getListener() {
return listener;
}
public static interface Listener {
public void onClick(FragmentManager fragmentManager);
}
}
public static class SubItem extends Item {
protected long count;
protected int color;
public SubItem(String title, Integer iconDrawableId, long count, int color, Listener listener) {
super(title, iconDrawableId, listener);
this.count = count;
this.color = color;
}
public long getCount() {
return count;
}
public void setCount(long count) {
this.count = count;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
}
notifyDataSetChanged() method is not working properly, sometimes getChildView is being called sometimes not,please help in order to fix this issue.

You are changing value to child Item however not setting back inside of Adapter.
SubItem item = ((SubItem) adapter.getChild(i+1, 5)).setCount(unreadBox);
adapter.setChild(i+1,5,item);
adapter.notifyDataSetChanged();
add this in Adapter
#Override
public void setChild(int groupPosition, int childPosition,SubItem item) {
subItems.get(groupPosition).get(childPosition) = item;
}

Related

multiple lists checkboxes getting messed up

I have 500+ items to put in a checklist divided into categories.
My solution so far for this is to use multiple expandableListView lists and the items inside them of course.
The problem shows up when I check some of the check boxes, the get messed up and check boxes from another lists get checked and I dont know why.. Can someone throw some light here?
ExpandableListView.java
public class IngredientsExpandableList extends ExpandableListActivity {
// Create ArrayList to hold parent Items and Child Items
private ArrayList<ParentModel> parentItems = new ArrayList<ParentModel>();
private ArrayList<ChildModel> childItems = new ArrayList<ChildModel>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create Expandable List and set it's properties
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(0);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
//Setting the data.
setData();
// Create the Adapter
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater(LayoutInflater.from(this), this);
// Set the Adapter to expandableList
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setData(){
String[] colors = getResources().getStringArray(R.array.ingredientsColor);
String[] innerColors = getResources().getStringArray(R.array.ingredientsInnerColor);
// Set the Items of Parent
for (int i = 0; i < 10; i++){
parentItems.add(new ParentModel("ingredients "+(i+1), Color.parseColor(colors[i])));
}
// Set The Child Data
ArrayList<String> child = null;
for (int i = 0; i < parentItems.size(); i++){
child = new ArrayList<String>();
for (int k = 0; k < 4; k++){
child.add("ingredient " + (k+1));
}
childItems.add(new ChildModel(child,Color.parseColor(innerColors[i])));
}
}
public void makeToast(String string){
Toast.makeText(this, string,
Toast.LENGTH_SHORT).show();
}
public class ParentModel{
String text;
int color;
public ParentModel(String text, int color) {
this.text = text;
this.color = color;
}
public String getText() {
return text;
}
public int getColor() {
return color;
}
}
public class ChildModel{
ArrayList<String> text;
int color;
public ChildModel(ArrayList<String> text, int color) {
this.text = text;
this.color = color;
}
public ArrayList<String> getText() {
return text;
}
public int getColor() {
return color;
}
}
public class MyExpandableAdapter extends BaseExpandableListAdapter
{
private Activity activity;
private ArrayList<ChildModel> childItems;
private LayoutInflater inflater;
private ArrayList<ParentModel> parentItems;
private ArrayList<String> child;
// constructor
public MyExpandableAdapter(ArrayList<ParentModel> parents, ArrayList<ChildModel> childern)
{
this.parentItems = parents;
this.childItems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity)
{
this.inflater = inflater;
this.activity = activity;
}
// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
child = childItems.get(groupPosition).getText();
TextView textView = null;
CheckBox checkBox = null;
LinearLayout LinearView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_list, null);
}
// get the textView reference and set the value
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
LinearView = (LinearLayout) convertView.findViewById(R.id.layout);
LinearView.setBackgroundColor(childItems.get(groupPosition).getColor());
checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
// set the ClickListener to handle the click event on child item
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
makeToast("clicked");
}
});
return convertView;
}
// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_list, null);
}
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setText(parentItems.get(groupPosition).getText());
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setBackgroundColor(parentItems.get(groupPosition).getColor());
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition)
{
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
#Override
public int getChildrenCount(int groupPosition)
{
return childItems.get(groupPosition).getText().size();
}
#Override
public Object getGroup(int groupPosition)
{
return null;
}
#Override
public int getGroupCount()
{
return parentItems.size();
}
#Override
public void onGroupCollapsed(int groupPosition)
{
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition)
{
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition)
{
return 0;
}
#Override
public boolean hasStableIds()
{
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
}
}
This is because views are recycled in lists so you must store the state of the checks and make sure you set them inside the get child view. something like this
public class IngredientsExpandableList extends ExpandableListActivity {
// Create ArrayList to hold parent Items and Child Items
private ArrayList<ParentModel> parentItems = new ArrayList<ParentModel>();
private ArrayList<ChildModel> childItems = new ArrayList<ChildModel>();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Create Expandable List and set it's properties
ExpandableListView expandableList = getExpandableListView();
expandableList.setDividerHeight(0);
expandableList.setGroupIndicator(null);
expandableList.setClickable(true);
//Setting the data.
setData();
// Create the Adapter
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
adapter.setInflater(LayoutInflater.from(this), this);
// Set the Adapter to expandableList
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(this);
}
public void setData(){
String[] colors = getResources().getStringArray(R.array.ingredientsColor);
String[] innerColors = getResources().getStringArray(R.array.ingredientsInnerColor);
// Set the Items of Parent
for (int i = 0; i < 10; i++){
parentItems.add(new ParentModel("ingredients "+(i+1), Color.parseColor(colors[i])));
}
// Set The Child Data
ArrayList<String> child = null;
for (int i = 0; i < parentItems.size(); i++){
child = new ArrayList<String>();
for (int k = 0; k < 4; k++){
child.add("ingredient " + (k+1));
}
childItems.add(new ChildModel(child,Color.parseColor(innerColors[i])));
}
}
public void makeToast(String string){
Toast.makeText(this, string,
Toast.LENGTH_SHORT).show();
}
public class ParentModel{
String text;
int color;
public ParentModel(String text, int color) {
this.text = text;
this.color = color;
}
public String getText() {
return text;
}
public int getColor() {
return color;
}
}
public class ChildModel{
ArrayList<String> text;
int color;
ArrayList<Boolean> checked = new ArrayList();;
public ChildModel(ArrayList<String> text, int color) {
this.text = text;
this.color = color;
for(String i:text)
{
checked.add(false);
}
}
public ArrayList<String> getText() {
return text;
}
public int getColor() {
return color;
}
}
public class MyExpandableAdapter extends BaseExpandableListAdapter
{
private Activity activity;
private ArrayList<ChildModel> childItems;
private LayoutInflater inflater;
private ArrayList<ParentModel> parentItems;
private ArrayList<String> child;
// constructor
public MyExpandableAdapter(ArrayList<ParentModel> parents, ArrayList<ChildModel> childern)
{
this.parentItems = parents;
this.childItems = childern;
}
public void setInflater(LayoutInflater inflater, Activity activity)
{
this.inflater = inflater;
this.activity = activity;
}
// method getChildView is called automatically for each child view.
// Implement this method as per your requirement
#Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
child = childItems.get(groupPosition).getText();
TextView textView = null;
CheckBox checkBox = null;
LinearLayout LinearView = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_list, null);
}
// get the textView reference and set the value
textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(child.get(childPosition));
LinearView = (LinearLayout) convertView.findViewById(R.id.layout);
LinearView.setBackgroundColor(childItems.get(groupPosition).getColor());
checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
checkBox.setChecked(childItems.get(groupPosition).checked.get(childPosition));
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
childItems.get(groupPosition).checked.get(childPosition) = isChecked;
}
});
// set the ClickListener to handle the click event on child item
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
makeToast("clicked");
}
});
return convertView;
}
// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_list, null);
}
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setText(parentItems.get(groupPosition).getText());
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setBackgroundColor(parentItems.get(groupPosition).getColor());
((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setChecked(isExpanded);
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition)
{
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
#Override
public int getChildrenCount(int groupPosition)
{
return childItems.get(groupPosition).getText().size();
}
#Override
public Object getGroup(int groupPosition)
{
return null;
}
#Override
public int getGroupCount()
{
return parentItems.size();
}
#Override
public void onGroupCollapsed(int groupPosition)
{
super.onGroupCollapsed(groupPosition);
}
#Override
public void onGroupExpanded(int groupPosition)
{
super.onGroupExpanded(groupPosition);
}
#Override
public long getGroupId(int groupPosition)
{
return 0;
}
#Override
public boolean hasStableIds()
{
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
}

ExpandableListview with single as well as multiple selection

[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;
}

Expandablelistview with checkboxes display in dialog

I'm writing filter in Android and want to use BaseExpandableListAdapter with checkboxes. In my situation if the group elements have any child elements, may select group element too. Also all child elements may selected. Offer screenshot:
enter image description here
When i open dialog and select neccessary elements, then its selected elements put to text edittext. My problem in this, when i reopen dialog and close/open group elements my selected checkboxes changes. I can't find solution how to remember choices, when reopen dialog.
All my neccessary code offer following:
My entity class:
public class Category implements Listable, Parcelable{
private int id;
private String title;
private String c_date;
private List<Category> sub_categories = new ArrayList<>();
private boolean isChecked;
private int parent;
protected Category(Parcel in) {
id = in.readInt();
title = in.readString();
c_date = in.readString();
sub_categories = in.createTypedArrayList(Category.CREATOR);
isChecked = in.readByte() != 0;
parent = in.readInt();
m_date = in.readString();
}
public static final Creator<Category> CREATOR = new Creator<Category>() {
#Override
public Category createFromParcel(Parcel in) {
return new Category(in);
}
#Override
public Category[] newArray(int size) {
return new Category[size];
}
};
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(title);
parcel.writeString(c_date);
parcel.writeTypedList(sub_categories);
parcel.writeByte((byte) (isChecked ? 1 : 0));
parcel.writeInt(parent);
parcel.writeString(m_date);
}
//getters, setters
}
My adapter class:
public class CategoryAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Category> categories;
private LayoutInflater inflater;
public CategoryAdapter(Context context,
ArrayList<Category> categoryList) {
this.context = context;
this.categories = categoryList;
inflater = LayoutInflater.from(context);
}
public Object getChild(int groupPosition, int childPosition) {
return categories.get(groupPosition).getSub_categories().get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return (long)( groupPosition*1024+childPosition ); // Max 1024 children per group
}
private static class ViewHolder {
private TextView title;
private CheckBox cb;
}
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder viewHolder;
boolean isChecked = categories.get(groupPosition).getSub_categories().get(childPosition).isChecked();
if (v != null)
viewHolder = (ViewHolder) v.getTag();
else {
v = inflater.inflate(R.layout.category_child_row, parent, false);
viewHolder = new ViewHolder();
viewHolder.title = (TextView)v.findViewById(R.id.title);
viewHolder.cb = (CheckBox)v.findViewById(R.id.check);
viewHolder.cb.setChecked(isChecked);
viewHolder.cb.setOnCheckedChangeListener(new CheckchangeChildListener(groupPosition, childPosition));
v.setTag(viewHolder);
}
final Category c = (Category) getChild( groupPosition, childPosition );
viewHolder.title.setText(c.getTitle());
return v;
}
class CheckchangeChildListener implements CompoundButton.OnCheckedChangeListener {
private int position;
private int childPosition;
public CheckchangeChildListener(int position, int childPosition) {
this.position= position;
this.childPosition = childPosition;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
categories.get(position).getSub_categories().get(childPosition).setChecked(true);
} else {
categories.get(position).getSub_categories().get(childPosition).setChecked(false);
}
}
}
public int getChildrenCount(int groupPosition) {
return categories.get(groupPosition).getSub_categories().size();
}
public Object getGroup(int groupPosition) {
return categories.get( groupPosition );
}
public int getGroupCount() {
return categories.size();
}
public long getGroupId(int groupPosition) {
return (long)( groupPosition*1024 ); // To be consistent with getChildId
}
private static class ViewGroupHolder {
private TextView title;
private CheckBox cb;
private ImageView image;
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View v = convertView;
ViewGroupHolder holder;
boolean isChecked = categories.get(groupPosition).isChecked();
if(v != null)
holder = (ViewGroupHolder)v.getTag();
else {
v = inflater.inflate(R.layout.category_parent_row, parent, false);
holder = new ViewGroupHolder();
holder.title = (TextView)v.findViewById(R.id.title);
holder.cb = (CheckBox)v.findViewById(R.id.check);
holder.cb.setChecked(isChecked);
holder.image = (ImageView)v.findViewById(R.id.arrow);
holder.cb.setOnCheckedChangeListener(new CheckchangeGroupListener(groupPosition));
v.setTag(holder);
}
final Category category = (Category)getGroup(groupPosition);
holder.title.setText(category.getTitle());
if(getChildrenCount(groupPosition) == 0){
holder.cb.setVisibility(View.VISIBLE);
holder.image.setVisibility(View.INVISIBLE);
} else {
holder.image.setVisibility(View.VISIBLE);
holder.cb.setVisibility(View.INVISIBLE);
holder.image.setImageResource(isExpanded ? R.drawable.ic_chevron_down : R.drawable.ic_chevron_right);
}
return v;
}
class CheckchangeGroupListener implements CompoundButton.OnCheckedChangeListener {
private int position;
public CheckchangeGroupListener(int position) {
this.position= position;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
categories.get(position).setChecked(true);
} else {
categories.get(position).setChecked(false);
}
}
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void onGroupCollapsed (int groupPosition) {}
public void onGroupExpanded(int groupPosition) {}
public ArrayList<Category> getSelectedCats(){
ArrayList<Category> checkedCategories = new ArrayList<>();
for(Category c: categories){
if(c.isChecked()) checkedCategories.add(c);
if(!c.getSub_categories().isEmpty()){
for(Category category: c.getSub_categories()) {
if (category.isChecked()) checkedCategories.add(category);
}
}
}
return checkedCategories;
}
}
My custom EditText:
public class CategoryEditText<T extends Listable> extends EditText {
List<T> mItems;
String[] mListableItems;
CharSequence mHint;
OnItemSelectedListener<T> onItemSelectedListener;
DialogFragment newFragment;
public CategoryEditText(Context context) {
super(context);
mHint = getHint();
}
public CategoryEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mHint = getHint();
}
public CategoryEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mHint = getHint();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CategoryEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mHint = getHint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setFocusable(false);
setClickable(true);
}
public void setItems(List<T> items) {
this.mItems = items;
newFragment = CategoryDialogFragment.newInstance((ArrayList<Category>)mItems, this);
this.mListableItems = new String[items.size()];
int i = 0;
for (T item : mItems) {
mListableItems[i++] = item.getLabel();
}
configureOnClickListener();
}
private void configureOnClickListener() {
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Context context = getContext();
FragmentManager fm = ((Activity) context).getFragmentManager();
newFragment.show(fm, "dialog");
}
});
}
public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) {
this.onItemSelectedListener = onItemSelectedListener;
}
public interface OnItemSelectedListener<T> {
void onItemSelectedListener(T item, int selectedIndex);
}
}
My custom dialogfragment to display list:
public class CategoryDialogFragment extends DialogFragment {
ArrayList<Category> categories;
static EditText editText;
public static CategoryDialogFragment newInstance(ArrayList<Category> categories, EditText customEditText) {
CategoryDialogFragment frag = new CategoryDialogFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("categories", categories);
frag.setArguments(args);
editText = customEditText;
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
categories = getArguments().getParcelableArrayList("categories");
View v = LayoutInflater.from(getActivity()).inflate(R.layout.select_category_layout, null);
ExpandableListView expandableListView = (ExpandableListView) v.findViewById(R.id.list);
final CategoryAdapter categoryAdapter = new CategoryAdapter(getActivity(), categories);
expandableListView.setAdapter(categoryAdapter);
MaterialDialog dialog = new MaterialDialog.Builder(getActivity())
.title(R.string.shop_category)
.customView(v, false)
.positiveText(R.string.select)
.negativeText(android.R.string.cancel)
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog materialDialog, #NonNull DialogAction dialogAction) {
ArrayList<Category> cats = categoryAdapter.getSelectedCats();
String selectedText = "";
for(int i=0; i<cats.size(); i++){
selectedText += cats.get(i).getTitle();
if(i+1 < cats.size()){
selectedText += " | ";
}
}
editText.setText(selectedText);
materialDialog.dismiss();
}
})
.build();
return dialog;
}
}
I tried to reading on the net about that but I didn`t find how to solve my problem.
The problem is your getView() method. I'd suggest reading up on how to correctly write a list adapter class.
private static class ViewHolder {
private TextView title;
private ChecBbox cb;
}
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
boolean isChecked = categories.get(groupPosition).getSub_categories().get(childPosition).isChecked();
if( convertView != null )
viewHolder = (ViewHolder) v.getTag();
else {
v = inflater.inflate(R.layout.category_child_row, parent, false);
viewHolder = new ViewHolder();
holder.title = (TextView)v.findViewById(R.id.title);
holder.cb = (CheckBox)v.findViewById(R.id.check);
holder.cb.setChecked(isChecked);
cb.setOnCheckedChangeListener(new CheckchangeChildListener(groupPosition, childPosition));
v.setTag(viewHolder);
}
final Category c = (Category) getChild( groupPosition, childPosition );
title.setText(c.getTitle());
return v;
}
Checked changed listener:
class CheckchangeGroupListener implements CompoundButton.OnCheckedChangeListener {
private int groupPosition, childPosition;
public CheckchangeGroupListener(int groupPosition, int childPosition) {
this.groupPosition= groupPosition;
this.childPosition = childPosition;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
categories.get(groupPosition).getSub_categories().get(childPosition).setChecked(true);
} else {
categories.get(groupPosition).getSub_categories().get(childPosition).setChecked(false);
}
}

MultiChoiceModeListener GridView selection not changing background color

I'm following this tutorial to implement a multiple select in my grid view, so far I can select the items but they are not changing the background color, I've debugged the code, and it seems that mSelection is always null, I don't Know why this is happening
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Child> child;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
private HashMap<Integer, Boolean> mSelection;
public GridAdapter(Context context, ArrayList<Child> childValues) {
mContext = context;
mSelection = new HashMap<Integer, Boolean>();
child = childValues;
}
#Override
public int getCount() {
return child.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
NetworkImageView i;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
i = (NetworkImageView) convertView
.findViewById(R.id.flag);
i.setImageUrl(String.valueOf(child.get(position).getImage()), imageLoader);
if (mSelection.get(position) != null) {
convertView.setBackgroundResource(R.color.bg);// this is a selected position so make it red
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text = (TextView) convertView.findViewById(R.id.label);
holder.text.setText(child.get(position).getName());
return convertView;
}
static class ViewHolder {
TextView text;
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
}
Expandable adapter:
adapter = new GridAdapter(context, listchild);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
gridView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
private int nr = 0;
#Override
public void onItemCheckedStateChanged(ActionMode mode, final int position, long id, boolean checked) {
if (checked) {
nr++;
adapter.setNewSelection(position, checked);
} else {
nr--;
adapter.removeSelection(position);
}
mode.setTitle(nr + " rows selected!");
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
StringBuilder sb = new StringBuilder();
Set<Integer> positions = adapter.getCurrentCheckedPosition();
for (Integer pos : positions) {
sb.append(" " + pos + ",");
}
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
nr = 0;
adapter.clearSelection();
}
});
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
gridView.setItemChecked(position, !adapter.isPositionChecked(position));
}
});
return convertView;
}

ExpandableListview Like TreeView Android

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.

Categories

Resources