Skip to content Skip to sidebar Skip to footer

Increment The Value Of Text In Expandable Listview

I have created the custom Expandablelistview.That list view contains plus and minus image view and one text view in child items.If i click the plus in one row means it increase the

Solution 1:

private ViewHolder viewHolder; // make it global

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    Child child = (Child) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflator = LayoutInflater.from(parent.getContext());
        convertView = inflator.inflate(R.layout.detail_list, null);
        viewHolder = new ViewHolder();

        viewHolder.tv = (TextView) convertView.findViewById(R.id.type);
        viewHolder.food_image = (ImageView) convertView.findViewById(R.id.food_image);
        viewHolder.minus = (ImageView) convertView.findViewById(R.id.minus);
        viewHolder.plus = (ImageView) convertView.findViewById(R.id.plus);
        viewHolder.item_count = (TextView) convertView.findViewById(R.id.count);


        convertView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.childName.setText(child.getChildName());
    viewHolder.item_count.setText(child.getCount());
    viewHolder.ivPlus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                Child modelChild = groups.get(groupPosition).getChildrens().get(childPosition);

                if(modelChild.getCount()>=0)  // set your count default 0 when you bind data initially 
                int count = modelChild.getCount() + 1;

                modelChild.setCount(count);
                modelChild.setChildName(modelChild.getChildName());
                // set your other items if any like above
                groups.get(groupPosition).getChildrens().set(childPosition, child);
                notifyDataSetChanged();
        }
    });
    viewHolder.ivMinus.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(count!=0) {
                Child modelChild = groups.get(groupPosition).getChildrens().get(childPosition);

                if(modelChild.getCount()>0)  // set your count default 0 when you bind data initially 
                int count = modelChild.getCount() - 1;

                modelChild.setCount(count);
                modelChild.setChildName(modelChild.getChildName());
                // set your other items if any like above
                groups.get(groupPosition).getChildrens().set(childPosition, child);
                notifyDataSetChanged();
            }
        }
    });

    return convertView;
}

Post a Comment for "Increment The Value Of Text In Expandable Listview"