MyWay
MyWay

Reputation: 1025

Control results of clicking on expandable listview

I need to implement expandable list view to my application. This list will be used to add some ingredients to pizza, so I need to increase the prize when the ingredient is checked and decrease when ingredient is unchecked.

I find the code for custom expandable list and modify to match to my needs.

thats the code for adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {



        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<customPizzaSendInfo>> _listDataChild;

        public ExpandableListAdapter(Context context, List<String> listDataHeader,
                HashMap<String, List<customPizzaSendInfo>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }

        @Override
        public Object getChild(int groupPosition, int childPosititon) {
            return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

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

            final customPizzaSendInfo childText = (customPizzaSendInfo) getChild(groupPosition, childPosition);


            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expander_list_item, null);
            }

            CheckBox txtListChild = (CheckBox) convertView
                    .findViewById(R.id.lblListItem);

            txtListChild.setText(childText.nameString);
            txtListChild.setChecked(childText.choosen);
            txtListChild.setOnCheckedChangeListener(new OnCheckedChangeListener() {             


                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked == true)
                    {

                        prizeSmall += childText.priceSmallDouble;
                        prizeBig += childText.priceBigDouble;
                        normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                        duzaButton.setText("Duża " + String.valueOf(prizeBig));
                        Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
                    }else
                    {
                        prizeSmall -= childText.priceSmallDouble;
                        prizeBig -= childText.priceBigDouble;
                        normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                        duzaButton.setText("Duża " + String.valueOf(prizeBig));
                        Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
                    }

                }
            });
            return convertView;
        }



        @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 int getGroupCount() {
            return this._listDataHeader.size();
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expander_list_group, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }


    }

this is the code for class which contain data customPizzaSendInfo

public class customPizzaSendInfo {

    public String nameString;
    public boolean choosen;
    public Double priceSmallDouble;
    public Double priceBigDouble;
    public customPizzaSendInfo(String nameString, boolean choosen, Double priceSmallDouble, Double priceBigDouble) {
        super();
        this.nameString = nameString;
        this.choosen = choosen;
        this.priceSmallDouble =  priceSmallDouble;
        this.priceBigDouble = priceBigDouble;
    }


}

there are two problems:

If i try to make on any click litener to monitoring the state of children, I got no reaction, so I decide to make it inside AdapterClass.

How to solve it

UPDATE I try to update this _listDataChild which keeps data about objects, so I modify the on checkChanged

@Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked == true)
                    {
                        //
                        //Toast.makeText(getApplicationContext(), Integer.toString(childPosition) + " " + childText.nameString, Toast.LENGTH_LONG).show();
                        //Toast.makeText(getApplicationContext(), _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).nameString, Toast.LENGTH_LONG).show();
                        _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = true;
                        prizeSmall += childText.priceSmallDouble;
                        prizeBig += childText.priceBigDouble;
                        normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                        duzaButton.setText("Duża " + String.valueOf(prizeBig));
                        Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
                    }else
                    {
                        _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = false;
                        prizeSmall -= childText.priceSmallDouble;
                        prizeBig -= childText.priceBigDouble;
                        normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                        duzaButton.setText("Duża " + String.valueOf(prizeBig));
                        Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
                    }


                }
            });

When i scroll the object become unchecked ...

SOLUTION How I solve it by add onclick listener in the ExpandableListAdapter and I use advice of @Abd El-Rahman El-Tamawy to update _listDataChild and it works like a charm, here is the code.

txtListChild.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 Toast.makeText(getApplicationContext(), Integer.toString(childPosition), Toast.LENGTH_LONG).show();
                 if(_listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen)
                 {
                     _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = false;
                 }
                 else
                 {
                     _listDataChild.get("Skonfiguruj Dodatki").get(childPosition).choosen = true;
                 }
            }
        });

Upvotes: 0

Views: 161

Answers (1)

Sami Eltamawy
Sami Eltamawy

Reputation: 10009

You Should save the state of the button in your HashMap also in your onClickListener to enable your adapter to redraw the final state from the HashMap when you collapse and expand the parent again as following

@Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked == true)
                {
                    childText.
                    prizeSmall += childText.priceSmallDouble;
                    prizeBig += childText.priceBigDouble;
                    normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                    duzaButton.setText("Duża " + String.valueOf(prizeBig));
                    Toast.makeText(getApplicationContext(), Double.toString(childText.priceSmallDouble), Toast.LENGTH_LONG).show();
                }else
                {
                    prizeSmall -= childText.priceSmallDouble;
                    prizeBig -= childText.priceBigDouble;
                    normalnaButton.setText("Normalna " +String.valueOf(prizeSmall));
                    duzaButton.setText("Duża " + String.valueOf(prizeBig));
                    Toast.makeText(getApplicationContext(), "unchecked", Toast.LENGTH_LONG).show();
                }
                //Get the HashMap Item of the current position and set all its value with the new values
                 this._listDataChild.get("yourCurrentKey").get(position).choosen = !this._listDataChild.get("yourCurrentKey").get(position)..choosen;
                 //Complete saving all your data

Upvotes: 1

Related Questions