Skip to content Skip to sidebar Skip to footer

Values Of Counter Changes After Scrolling Expendablelistview

I have an ExpandableListView of items and on list item I have TextView with two buttons to increment or decrements the value in TextView on clicks. The problem occurs every time I

Solution 1:

It is not a good idea to store quantity in ViewHolder. Hope below sample helps :)

MainActivity.java:

publicclassMainActivityextendsActivity {

Button clearChecks, putOrder;
ExpandableListView expandableListView;
ExpandableListViewAdapter expandableListAdapter;
int lastExpandedPosition = -1;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

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

    expandableListView.setOnGroupExpandListener(newExpandableListView.OnGroupExpandListener() {
        @OverridepublicvoidonGroupExpand(int groupPosition) {
            if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
                expandableListView.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
        }
    });
    clearChecks.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            expandableListAdapter.clearChecks();
        }
    });
    putOrder.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            ArrayList<ChildItemSample> putOrder = expandableListAdapter.getOrderList();
            String msg = "";
            for(int i=0; i<putOrder.size(); i++){
                msg += putOrder.get(i).getName() + ": " + putOrder.get(i).getQty() + "\n";
            }
            Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
        }
    });
}

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

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

ChildItemSample.java:

publicclassChildItemSample {
privateboolean checked = false;
privateString name;
private int qty;

public int getQty() {
    return qty;
}

publicvoidsetQty(int qty) {
    this.qty = qty;
}

publicbooleanisChecked() {
    return checked;
}

publicvoidsetChecked(boolean checked) {
    this.checked = checked;
}

publicStringgetName() {
    return name;
}

publicChildItemSample(String name, int qty){
    this.name = name;
    this.qty = qty;
}
}

ExpandableListViewAdapter.java:

publicclassExpandableListViewAdapterextendsBaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
privateint checkedBoxesCount;
privateboolean[] checkedGroup;

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

@OverridepublicintgetGroupCount() {
    return listGroup.size();
}

@OverridepublicintgetChildrenCount(int groupPosition) {
    return listChild.get(listGroup.get(groupPosition)).size();
}

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

@Overridepublic ChildItemSample getChild(int groupPosition, int childPosition) {
    return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}

@OverridepubliclonggetGroupId(int groupPosition) {
    return groupPosition;
}

@OverridepubliclonggetChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@OverridepublicbooleanhasStableIds() {
    returnfalse;
}

@Overridepublic View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
    StringitemGroup= getGroup(groupPosition);
    GroupViewHolder groupViewHolder;
    if(view == null){
        LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.expanded_list_group, null);
        groupViewHolder = newGroupViewHolder();
        groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
        groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
        groupViewHolder.cbGroup.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                intpos= (int)view.getTag();
                checkedGroup[pos] = !checkedGroup[pos];
                for(ChildItemSample item : listChild.get(listGroup.get(pos))){
                    item.setChecked(checkedGroup[pos]);
                }
                notifyDataSetChanged();
            }
        });
        view.setTag(groupViewHolder);
    }else {
        groupViewHolder = (GroupViewHolder)view.getTag();
    }
    groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, getChildrenCount(groupPosition)));
    if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
    else groupViewHolder.cbGroup.setChecked(false);
    groupViewHolder.cbGroup.setTag(groupPosition);
    return view;
}

@Overridepublic View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
    ChildItemSampleexpandedListText= getChild(groupPosition,childPosition);
    ChildViewHolder childViewHolder;
    if(view == null){
        LayoutInflaterinflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.expanded_list_item, null);
        childViewHolder = newChildViewHolder();
        childViewHolder.tvChild = view.findViewById(R.id.tv_child);
        childViewHolder.cbChild = view.findViewById(R.id.cb_child);
        childViewHolder.tvQty = view.findViewById(R.id.tv_qty);
        childViewHolder.btInc = view.findViewById(R.id.bt_inc);
        childViewHolder.btDec = view.findViewById(R.id.bt_dec);
        childViewHolder.cbChild.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                CheckBoxcb= (CheckBox) view;
                Pospos= (Pos) cb.getTag();
                ChildItemSampleselectedItem= getChild(pos.group, pos.child);
                selectedItem.setChecked(cb.isChecked());
                if(cb.isChecked()){
                    checkedBoxesCount++;
                    Toast.makeText(context,"Checked value is: " + getChild(pos.group, pos.child).getName(),
                            Toast.LENGTH_SHORT).show();
                }else {
                    checkedBoxesCount--;
                    if(checkedBoxesCount == 0){
                        Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
                    }
                }
                notifyDataSetChanged();
            }
        });
        childViewHolder.btInc.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                Buttonbt= (Button) view;
                Pospos= (Pos) bt.getTag();
                ChildItemSampleselectedItem= getChild(pos.group, pos.child);
                selectedItem.setQty(selectedItem.getQty() + 1);
                notifyDataSetChanged();
            }
        });
        childViewHolder.btDec.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                Buttonbt= (Button) view;
                Pospos= (Pos) bt.getTag();
                ChildItemSampleselectedItem= getChild(pos.group, pos.child);
                if(selectedItem.getQty() > 0) selectedItem.setQty(selectedItem.getQty() - 1);
                notifyDataSetChanged();
            }
        });
    }else {
        childViewHolder = (ChildViewHolder)view.getTag();
    }
    childViewHolder.cbChild.setChecked(expandedListText.isChecked());
    childViewHolder.tvChild.setText(expandedListText.getName() + " :");
    childViewHolder.tvQty.setText("" + expandedListText.getQty());

    childViewHolder.cbChild.setTag(newPos(groupPosition, childPosition));
    childViewHolder.btInc.setTag(newPos(groupPosition, childPosition));
    childViewHolder.btDec.setTag(newPos(groupPosition, childPosition));
    view.setTag(childViewHolder);
    return view;
}

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

@OverridepublicbooleanisChildSelectable(int groupPosition, int childPosition) {
    returntrue;
}

privateclassGroupViewHolder {
    CheckBox cbGroup;
    TextView tvGroup;
}

privateclassChildViewHolder {
    CheckBox cbChild;
    TextView tvChild;
    TextView tvQty;
    Button btInc;
    Button btDec;
}

privateclassPos {
    int group;
    int child;

    Pos(int group, int child){
        this.group = group;
        this.child = child;
    }
}

public ArrayList<ChildItemSample> getOrderList(){
    ArrayList<ChildItemSample> overallOrder = newArrayList<>();
    for(int i=0; i<getGroupCount(); i++){
        for(int j=0; j<getChildrenCount(i); j++){
            if(getChild(i,j).getQty() > 0){
                ChildItemSamplenewOrder=newChildItemSample(getGroup(i) + ">" +
                        getChild(i, j).getName(), getChild(i, j).getQty());
                overallOrder.add(newOrder);
            }
        }
    }
    return overallOrder;
}
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/btnClearChecks"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Clear Checks" /><Buttonandroid:id="@+id/btnPutOrder"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="Put Order" /></LinearLayout><ExpandableListViewandroid:id="@+id/expandedListView"android:layout_width="match_parent"android:layout_height="match_parent"></ExpandableListView></LinearLayout>

expanded_list_group.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:descendantFocusability="blocksDescendants" ><CheckBoxandroid:id="@+id/cb_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="40dp"android:layout_gravity="center_vertical" /><TextViewandroid:id="@+id/tv_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Text"android:textSize="30sp" /></LinearLayout>

expanded_list_item.xml:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content" ><CheckBoxandroid:id="@+id/cb_child"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_centerVertical="true"android:layout_marginLeft="60dp" /><TextViewandroid:id="@+id/tv_child"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toRightOf="@+id/cb_child"android:text="Child: "android:textSize="20sp" /><TextViewandroid:id="@+id/tv_qty"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toRightOf="@+id/tv_child"android:text="0"android:textSize="20sp" /><Buttonandroid:id="@+id/bt_inc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_toLeftOf="@+id/bt_dec"android:text="+" /><Buttonandroid:id="@+id/bt_dec"android:layout_marginRight="10dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:text="-" /></RelativeLayout>

Solution 2:

Try this adapter:

publicclassExpandableListAdapterextendsBaseExpandableListAdapter {

classViewHolder {
    TextView childText;
    TextView counterText;
    Button addItemButton;
    Button deleteItemButton;
}

classChildItem{
    String name;
    int quantity;
    ChildItem(String name, int quantity){
        this.name = name;
        this.quantity = quantity;
    }
}

classPos{
    int group;
    int child;
    Pos(int group, int child){
        this.group = group;
        this.child = child;
    }
}

private Context context;
private List<String> listDataHeader;
//private HashMap<String, List<String>> listHashMap;private HashMap<String, List<ChildItem>> listChildMap;

publicExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
    this.context = context;
    this.listDataHeader = listDataHeader;
    listChildMap = newHashMap<>();
    for(int i=0; i<getGroupCount(); i++){
        List<ChildItem> listTemp = newArrayList<>();
        for(int j=0; j<listHashMap.get(listDataHeader.get(i)).size(); j++){
            listTemp.add(newChildItem(listHashMap.get(listDataHeader.get(i)).get(j), 0));
        }
        listChildMap.put(listDataHeader.get(i), listTemp);
    }
}

@OverridepublicintgetGroupCount() {
    return listDataHeader.size();
}

@OverridepublicintgetChildrenCount(int groupPosition) {
    return listChildMap.get(listDataHeader.get(groupPosition)).size();
}

@Overridepublic String getGroup(int groupPosition) {
    return listDataHeader.get(groupPosition);
}

@Overridepublic ChildItem getChild(int groupPosition, int childPosition) {
    return listChildMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}

@OverridepubliclonggetGroupId(int groupPosition) {
    return groupPosition;
}

@OverridepubliclonggetChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@OverridepublicbooleanhasStableIds() {
    returnfalse;
}

@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    StringheaderTitle= getGroup(groupPosition);
    if(convertView == null) {
        LayoutInflaterinflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_group, null);
    }
    TextViewlistHeader= (TextView) convertView.findViewById(R.id.list_header);
    listHeader.setTypeface(null, Typeface.BOLD);
    listHeader.setText(headerTitle);
    return convertView;
}

@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if(convertView == null) {
        LayoutInflaterinflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.list_item, null);
        TextViewtextListChild= (TextView) convertView.findViewById(R.id.list_item_header);
        TextViewitemsCounter= (TextView) convertView.findViewById(R.id.items_counter);
        ButtonaddItemButton= (Button) convertView.findViewById(R.id.plus_btn);
        viewHolder = newViewHolder();
        viewHolder.childText = textListChild;
        viewHolder.counterText = itemsCounter;
        viewHolder.addItemButton = addItemButton;
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    ChildItemchild= getChild(groupPosition, childPosition);
    viewHolder.childText.setText(child.name);
    viewHolder.counterText.setText("" + child.quantity);
    viewHolder.addItemButton.setOnClickListener(newView.OnClickListener(){
        @OverridepublicvoidonClick(View v) {
            Pospos= (Pos)v.getTag();
            ChildItemselectedItem= getChild(pos.group, pos.child);
            selectedItem.quantity = selectedItem.quantity + 1;
            notifyDataSetChanged();

            PutOrderDrinks.addOrder(selectedItem.name);
        }
    });
    viewHolder.addItemButton.setTag(newPos(groupPosition, childPosition));
    convertView.setTag(viewHolder);
    return convertView;
}

@OverridepublicbooleanisChildSelectable(int groupPosition, int childPosition) {
    returntrue;
}
}

Post a Comment for "Values Of Counter Changes After Scrolling Expendablelistview"