3

I have a problem in Expandable listview while scrolling, edittext value changes.I have tried to store values in modal class and set that value while view create.But Edittext values changes while scrolling.Please suggest any solution.Here is my code for expandable listview.Give me some hint or where i make error in this code.

Below is my logs from android studio

11-04 14:31:59.826 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{48bc9e8 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos0 11-04 14:32:04.862 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{a67b858 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos4 11-04 14:32:04.911 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{48bc9e8 VFED..CL. ......ID 0,0-514,118 #7f080061 app:id/edtQty} Pos5 11-04 14:32:04.932 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{39f8d9c VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos6 11-04 14:32:05.681 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{d8d264 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos3 11-04 14:32:05.738 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{bd81fa0 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos2 11-04 14:32:05.815 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{39f8d9c VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos1 11-04 14:32:05.879 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{48bc9e8 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos0 11-04 14:32:06.201 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{a67b858 VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos4 11-04 14:32:06.235 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{39f8d9c VFED..CL. ........ 0,0-514,118 #7f080061 app:id/edtQty} Pos5 11-04 14:32:06.289 3632-3632/com.codefuelindia.healthcircleordermgmt E/edittext:-: android.support.design.widget.TextInputEditText{48bc9e8 VFED..CL. ......ID 0,0-514,118 #7f080061 app:id/edtQty} Pos6

The edittext reference duplicates.How to solve?

import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    import android.content.Context;
    import android.graphics.Typeface;
    import android.support.design.widget.TextInputEditText;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseExpandableListAdapter;
    import android.widget.TextView;

    import com.codefuelindia.healthcircleordermgmt.R;
    import com.codefuelindia.healthcircleordermgmt.model.CatMaster;
    import com.codefuelindia.healthcircleordermgmt.model.ProChild;

    public class ExpandableListAdapter extends BaseExpandableListAdapter {

        private Context _context;
        private ArrayList<CatMaster> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<CatMaster, ArrayList<ProChild>> _listDataChild;

        public ExpandableListAdapter(Context context, ArrayList<CatMaster> listDataHeader,
                HashMap<CatMaster, ArrayList<ProChild>> 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(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            final ProChild childText = (ProChild) 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);


             TextView tvUnitPrice;
             final TextInputEditText edtQty;
             final TextView tvTotalPrice;

            tvUnitPrice = (TextView) convertView.findViewById(R.id.tvUnitPrice);
            edtQty = (TextInputEditText) convertView.findViewById(R.id.edtQty);
            edtQty.setText();
            tvTotalPrice = (TextView) convertView.findViewById(R.id.tvTotalPrice);

            txtListChild.setText(childText.getName());
            tvUnitPrice.setText(childText.getPrice());
            edtQty.setText(childText.getQty());

            edtQty.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

f(!TextUtils.isEmpty(charSequence.toString())){
                    childText.setQty(charSequence.toString());

                   final double total=Double.parseDouble(childText.getPrice())*Integer.parseInt(charSequence.toString());
                    tvTotalPrice.setText("Total: ".concat(String.valueOf(total)));
                }
                else {
                    tvTotalPrice.setText("");

                }

                }

                @Override
                public void afterTextChanged(Editable editable) {



                }
            });


            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) {
            CatMaster headerTitle = (CatMaster) getGroup(groupPosition);
            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.getCatName());

            return convertView;
        }

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

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
Nirav Shah
  • 263
  • 3
  • 12
  • in getGroupView. you need to get groupview object and set text to it not to direct convertview ? – Amod Gokhale Nov 03 '17 at 09:30
  • Not working..please give some working answer..the editext assign to multiple edittext.Same value reflet in other edittext while scrolling – Nirav Shah Nov 03 '17 at 11:48
  • @Amod Gokhale I have edittext in child view.So How can I manage while scrolling? – Nirav Shah Nov 04 '17 at 03:55
  • i thought adding groupView should fix your problem but if not you might want to try this tutorial and compare your code with it http://www.vogella.com/tutorials/AndroidListView/article.html#expandablelistview – Amod Gokhale Nov 04 '17 at 07:19

2 Answers2

1

After debugging and analysis of onchildView() I found solution that work.. I have created viewholder for store parent and child view

class ViewHolder {
//Log.e("text view",txtListChild.toString());
        TextView lblListHeader;
        HashMap<Integer, HashMap<Integer, View>> multiHash = new HashMap<>();
        HashMap<Integer, View> innerHashMap;


        public ViewHolder(View v) {
            innerHashMap = new HashMap<>();
            lblListHeader = v.
                    findViewById(R.id.lblListHeader);


        }

        public View getInnerView(int pos, int groupPos) {
            return multiHash.get(groupPos).get(pos);
        }


//        lblListHeader.setTypeface(null, Typeface.BOLD);
//        lblListHeader.setText(headerTitle.getCatName());

        public void setInnerView(int pos, int groupPos, View innerView) {

            innerHashMap.put(pos, innerView);
            multiHash.put(groupPos, innerHashMap);


        }
    }

and setter and getter method to store and retrieve view from hashmap.if view is null i set the view to hashmap and othervise get the view from hashmap which i have store.Hope Other developer who face this issue get help from this answer.Have a nice day..

@Override

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

        final ProChild childText = (ProChild) getChild(groupPosition, childPosition);
        View myConvertView = convertView;


        // Log.e("quantity:- ",childText.getQty().concat(String.valueOf(childPosition)));
        final TextInputEditText textInputEditText;

        if (convertView == null || !isLastChild || childPosition==this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size()-1) {

            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
            holder.setInnerView(childPosition, groupPosition, convertView);



            TextInputEditText textInputEditText1 = convertView.findViewById(R.id.edtQty);
            TextView tvUnitPrice = convertView.findViewById(R.id.tvUnitPrice);
            final TextView tvTotalPrice = convertView.findViewById(R.id.tvTotalPrice);
            final TextView tvHeader=convertView.findViewById(R.id.lblListItem);

            tvHeader.setText(childText.getName());
            textInputEditText1.setText(childText.getQty());
            tvUnitPrice.setText(childText.getPrice());
            tvTotalPrice.setText( String.valueOf(childText.getTotal()));
            //holder.edtQty.setText(childText.getQty());

//        edtQty.setText(_listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition).getQty());


            textInputEditText1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    //Log.e("child...",""+childPosition);
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    grandTotalOf = 0.0;
                    childText.setQty(editable.toString());
//                  _listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition).setQty(editable.toString());

                    if (!editable.toString().isEmpty() && !editable.toString().equals("0")) {
                        //final double total=Double.parseDouble();

                        final double total = Double.parseDouble(childText.getPrice()) * Integer.parseInt(editable.toString());
                        tvTotalPrice.setText("Total: ".concat(String.valueOf(total)));
                        childText.setTotal(total);

                    } else {
                        tvTotalPrice.setText("");
                        childText.setTotal(0);
                        grandTotal.setGrandTotal(0);

                    }


                    for(int i=0;i<_listDataHeader.size();i++){

                        for(int j=0;j<_listDataChild.get(_listDataHeader.get(i)).size();j++){

                            grandTotalOf=grandTotalOf + _listDataChild.get(_listDataHeader.get(i)).get(j).getTotal();
                        }


                    }


                    grandTotal.setGrandTotal(grandTotalOf);

                }
            });


            return holder.getInnerView(childPosition, groupPosition);

        } else {




            convertView = holder.getInnerView(childPosition, groupPosition);

            if (convertView == null) {
                holder.setInnerView(childPosition, groupPosition, myConvertView);

                convertView = holder.getInnerView(childPosition, groupPosition);
            }


            TextInputEditText textInputEditText1 = convertView.findViewById(R.id.edtQty);
            TextView tvUnitPrice = convertView.findViewById(R.id.tvUnitPrice);
            final TextView tvTotalPrice = convertView.findViewById(R.id.tvTotalPrice);
            final TextView tvHeader=convertView.findViewById(R.id.lblListItem);

          textInputEditText1.setText(childText.getQty());
            tvUnitPrice.setText(childText.getPrice());
            tvHeader.setText(childText.getName());
            tvTotalPrice.setText( String.valueOf(childText.getTotal()));


       edtQty.setText(_listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition).getQty());


            textInputEditText1.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    //Log.e("child...",""+childPosition);
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {

                    grandTotalOf=0.0;
                    childText.setQty(editable.toString());
//                  _listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition).setQty(editable.toString());

                    if (!editable.toString().isEmpty() && !editable.toString().equals("0")) {
                        //final double total=Double.parseDouble();

                        final double total = Double.parseDouble(childText.getPrice()) * Integer.parseInt(editable.toString());
                        tvTotalPrice.setText("Total: ".concat(String.valueOf(total)));
                        childText.setTotal(total);
                        grandTotal.setGrandTotal(total);

                    } else {
                        tvTotalPrice.setText("");
                        childText.setTotal(0);
                        grandTotal.setGrandTotal(0);

                    }


                    for(int i=0;i<_listDataHeader.size();i++){

                        for(int j=0;j<_listDataChild.get(_listDataHeader.get(i)).size();j++){

                            grandTotalOf=grandTotalOf + _listDataChild.get(_listDataHeader.get(i)).get(j).getTotal();
                        }


                    }


                    grandTotal.setGrandTotal(grandTotalOf);

                }
            });

            return convertView;
        }





    }
Nirav Shah
  • 263
  • 3
  • 12
-1

while scrolling, edittext value changes. This is Property of list container in android(List view or recycler View) . Your data will recycle after scroll . So if you need to maintain the state of your view just save if inside your data set . For instance : In your case you need to maintain the previously typed text during scrolling .

 edtQty.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if(!TextUtils.isEmpty(charSequence.toString())){
            childText.setValue(charSequence.toString());
            final double total=Double.parseDouble(childText.getPrice())*Integer.parseInt(charSequence.toString());
            tvTotalPrice.setText("Total: ".concat(String.valueOf(total)));
        }
        else {
            tvTotalPrice.setText("");

        }

    }

    @Override
    public void afterTextChanged(Editable editable) {
    }
});

Then set the previously saved text for each new child returned:

 public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {
    et_Search.setText(dataset.get(position).Val);
}

In edition Edittext on each list item is wrong design practice. I suggest you come up with better UI design . For instance you can have a button on list item and on click of button you can take input with the help of a dialog and save the previous state too . Read This.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • not working..which position i have to take child or parent ? – Nirav Shah Nov 03 '17 at 08:27
  • Well obviously where exactly your edit text is located . and set the value in your ProChild(Only if Edit text is in child view) item – ADM Nov 03 '17 at 08:38
  • Edited my answer . It maybe not the complete logical solution . But you will get the idea . – ADM Nov 03 '17 at 08:44
  • I had done as you said set value in ProChild(Edit text is in child view) But value gets changes.Any Idea.? – Nirav Shah Nov 03 '17 at 09:13
  • Print Logs in OnChild() and see what happening . See the updated answer too. – ADM Nov 03 '17 at 09:28
  • i edited my question and put OnChild() logs in that.Any suggestion or solution? – Nirav Shah Nov 06 '17 at 04:37
  • The solution above should work. I recommend you to read the last 3 line of my answer. For best user experience You must follow the link i have attached in this scenario . – ADM Nov 06 '17 at 05:07
  • @Adam I have solve that problem which i explain above.Thank for reply and giving suggestion. – Nirav Shah Nov 06 '17 at 09:36