Search code examples
androidexpandablelistviewexpandablelistadaptertextwatcher

Multiple TextWatcher added to same EditText in ExpandableListView's group view


From the conclusion of previous post I am not able to track how the TextWatcher is being added multiple times to EditText in the ExpandableListView. The ArrayList has only 2 elements.

The output should list only for 0 and 1 once but it was called twice.

Adapter:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

     private LayoutInflater inflater;
     private Context context;
     private ExpandableListView accordion;
     private int lastExpandedGroupPosition; 
     ArrayList<ModelObject> mParent;    
     TextView textViewLabelGrandTotal;
     float grandTotal = 0;

     public ExpandableListAdapter( Context context, ArrayList<ModelObject> ModelObject, ExpandableListView accordion, TextView textViewLabelGrandTotal)
     {

         mParent = ModelObject;
         inflater = LayoutInflater.from(context);
         this.accordion = accordion;   
         this.context=context;   
         this.textViewLabelGrandTotal=textViewLabelGrandTotal;
     }

     @Override
     //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

     @Override
     //counts the number of children items so the list knows how many times calls getChildView() method
     public int getChildrenCount(int i) {
         return mParent.get(i).childCount;
     }

     @Override
     //gets the title of each parent/group
     public Object getGroup(int i) {
         return mParent.get(i).INVOICE_ID;
     }

     @Override
     //gets the name of each item
     public Object getChild(int i, int i1) {
            return mParent.get(i).children.get(i1);
     }

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

     @Override
     public long getChildId(int i, int i1) {
            return i1;
     }

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

     @Override
     //in this method you must set the text to see the parent/group on the list
     public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
         if (view == null) {
                view = inflater.inflate(R.layout.sfa_receipt_by_customer_new_receipt_due_invoice_list_item_parent, viewGroup,false);
         } 
        // set category name as tag so view can be found view later
        view.setTag(getGroup(i).toString());

        CheckBox CheckBoxInv =(CheckBox)view.findViewById(R.id.CheckBoxInv);
        TextView textViewLabelInvoice = (TextView) view.findViewById(R.id.textViewLabelInvoice);
        TextView textViewLabelDate = (TextView) view.findViewById(R.id.textViewLabelDate);
        TextView textViewDueAmt = (TextView) view.findViewById(R.id.textViewDueAmt);
        TextView textViewRemainingAmt = (TextView) view.findViewById(R.id.textViewRemainingAmt);
        final EditText editTextPaid = (EditText)view.findViewById(R.id.editTextPaid);

        DecimalFormat df = new DecimalFormat("#.##");
        textViewLabelInvoice.setText(mParent.get(i).INVOICE_NO);
        textViewLabelDate.setText(mParent.get(i).INVOICE_DATE);
        textViewDueAmt.setText(df.format(mParent.get(i).DUE));
        textViewRemainingAmt.setText(df.format(mParent.get(i).REMAINING_AFTER_PAID));
        CheckBoxInv.setChecked(mParent.get(i).checked);

        CheckBoxInv.setOnClickListener(new View.OnClickListener() {         

            @Override
            public void onClick(View v) {

                CheckBox c = (CheckBox) v;

                System.out.println("===================== check change listerner ============================");
                editTextPaid.setText(df.format(mParent.get(i).DUE)); // editTextPaid.setText("");
                System.out.println("===================== check change listerner ============================");

                if(c.isChecked() == false)
                {
                    System.out.println("===================== uncheck change listerner ============================");
                    editTextPaid.setText("");
                    System.out.println("===================== uncheck change listerner ============================");
                }
//              notifyDataSetChanged();
            }
        });

        //enable focus of edit text box
        editTextPaid.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                System.out.println("ON TOUCH LISTERNER");
                return false;
            }
        });

        //disable focus of edittext box
        editTextPaid.setOnFocusChangeListener(new View.OnFocusChangeListener() {          

            public void onFocusChange(View v, boolean hasFocus) {
                System.out.println("ON FOCUS CHANGE LISTENER");
            }
        });

        //re calculate the remaining balance amount
        editTextPaid.addTextChangedListener(new TextWatcher() {

            {
                System.out.println("TextWatcher Initialized..................!! " + i + " " + this);
            }


            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                System.out.println("ON TEXT CHANGE");

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                System.out.println("BEFORE TEXT CHANGE");
            }

            @Override
            public void afterTextChanged(Editable arg0) {

                System.out.println("AFTER TEXT CHANGE");

            }
        });


        //return the entire view
        return view;                
     }


     @Override
     //in this method you must set the text to see the children on the list
     public View getChildView(int i,  int i1, boolean b, View view, ViewGroup viewGroup) {
          if (view == null) 
          {
                view = inflater.inflate(R.layout.sfa_receipt_by_customer_new_receipt_due_invoice_list_item_child, viewGroup,false);       
          }
          System.out.println("Inside getChildView()..................!!"); 
          TextView textViewLabelReceipt = (TextView) view.findViewById(R.id.textViewLabelReceipt);
          textViewLabelReceipt.setText((i1+1)+"."+mParent.get(i).children.get(i1).RECEIPT_NO);

          TextView textViewLabelDate = (TextView) view.findViewById(R.id.textViewLabelDate);
          textViewLabelDate.setText((i1+1)+"."+mParent.get(i).children.get(i1).RECEIPT_DATE);

          TextView textViewRemainingAmt = (TextView) view.findViewById(R.id.textViewRemainingAmt);
          textViewRemainingAmt.setText((i1+1)+"."+mParent.get(i).children.get(i1).REMAINING_AFTER_ADJUSTED_AMOUNT);

          TextView textViewDueAmt = (TextView) view.findViewById(R.id.textViewDueAmt);
          textViewDueAmt.setText((i1+1)+"."+mParent.get(i).children.get(i1).ADVANCE_BALANCE);

          EditText editTextPaid =(EditText)view.findViewById(R.id.editTextPaid);

          return view;
     }

     @Override
     public boolean isChildSelectable(int i, int i1) {
            return true;
     }

     @Override
     /**
     * automatically collapse last expanded group    
     */    
     public void onGroupExpanded(int groupPosition) {

            if(groupPosition != lastExpandedGroupPosition){
                accordion.collapseGroup(lastExpandedGroupPosition);
            }           
            super.onGroupExpanded(groupPosition);        
            lastExpandedGroupPosition = groupPosition;          
     }   

}

OUTPUT:

07-30 06:59:28.219: I/System.out(9082): TextWatcher Initialized..................!! 0 com.example.ExpandableListAdapter$4@52bf1224
07-30 06:59:28.223: I/System.out(9082): TextWatcher Initialized..................!! 1 com.example.ExpandableListAdapter$4@52c09858
07-30 06:59:28.227: I/System.out(9082): TextWatcher Initialized..................!! 0 com.example.ExpandableListAdapter$4@52c37d10
07-30 06:59:28.239: I/System.out(9082): TextWatcher Initialized..................!! 1 com.example.ExpandableListAdapter$4@52c723ec

Solution

  • Solved it myself.

    1. Placed all the view[CHECKBOX, EDITTEXT, TEXTVIEW] for the getGroupView() method within ViewHolder class [which is nothing but a class which contain views].

    2. Saved it using settag() method of the "view" object which is a parameter of the getGroupView() method if the "view" parameter returned null.

    3. Retrieved the views using getTag() and initialized the ViewHolder object from it if the "view" parameter is not null.