EditText TextChangeListener called multiple times

2019-09-07 05:59发布

In this particular scenario the ExpandableList is suppose to show 2 list as group. When a user selects the checkbox, it add the value to the EditText and thats where the TextChangeListener is fired up 3 times for the first input and 1 time for the second as shown in the output section.

The inputs received by the EditText on check change were

22448 for the 0 item in the arraylist

&

56.12 for the 1st item in the arraylist

Code

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() {

            @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 04:06:47.662    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:06:47.662    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0:  arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:47.662    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0:  arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:47.666    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0:  arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:47.666    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:47.666    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:47.666    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:47.666    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:06:58.822    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:06:58.822    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.822    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.822    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.826    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.826    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.826    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.826    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.826    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.830    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.830    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:06:58.830    1432-1432/? I/System.out﹕ ===================== uncheck change listerner ============================
07-30 04:06:58.830    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.830    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.830    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.834    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0:  arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.834    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0:  arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.834    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0:  arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter$4@52a697d0
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter$4@528a1cfc
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter$4@52882534
07-30 04:06:58.834    1432-1432/? I/System.out﹕ ===================== uncheck change listerner ============================
07-30 04:07:03.337    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:07:03.337    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0:  arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:03.337    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 0 arg3: 9 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:03.337    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:03.337    1432-1432/? I/System.out﹕ com.example.ExpandableListAdapter@52ae033c : ===================== check change listerner ============================
07-30 04:07:06.053    1432-1432/? I/System.out﹕ com.example.ExpandableListAdapter@52ae033c : ===================== check change listerner ============================
07-30 04:07:06.057    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.057    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 9 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.057    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.057    1432-1432/? I/System.out﹕ ===================== check change listerner ============================
07-30 04:07:06.057    1432-1432/? I/System.out﹕ ===================== uncheck change listerner ============================
07-30 04:07:06.057    1432-1432/? I/System.out﹕ BEFORE TEXT CHANGE >>> arg0: Some text arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.057    1432-1432/? I/System.out﹕ ON TEXT CHANGE >>> arg0:  arg1: 0 arg2: 9 arg3: 0 this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.085    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter$4@52bd946c
07-30 04:07:06.085    1432-1432/? I/System.out﹕ ===================== uncheck change listerner ============================

ExpandableList GroupView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list_item">

  <CheckBox 
         android:id="@+id/CheckBoxInv"
         android:layout_width="wrap_content"
         android:focusable="false"                            
         android:layout_height="wrap_content" />

     <TextView  
         android:id="@+id/textViewLabelInvoice"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textStyle="bold"
         android:layout_toRightOf="@+id/CheckBoxInv"
         android:text="IV14AA00040000001"       
         />

      <TextView
          android:id="@+id/textViewLabelDate"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewDueAmt"
          android:layout_toRightOf="@+id/CheckBoxInv"
          android:text="15.12.2014" />

       <TextView
          android:id="@+id/textViewLabelRemainingAmt"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewDueAmt"
          android:layout_toRightOf="@+id/textViewLabelDate"
          android:layout_marginLeft="5dp"
          android:text="Remaining" />

       <TextView
          android:id="@+id/textViewRemainingAmt"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/textViewDueAmt"
          android:layout_alignParentRight="true"
          android:layout_marginLeft="5dp"
          android:text="1000.00" />

     <TextView  
         android:id="@+id/textViewDueAmt"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:textStyle="bold"
         android:textSize="16sp"
         android:text="10000.00"        
         />

     <EditText
         android:id="@+id/editTextPaid"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_below="@+id/textViewLabelDate"
         android:focusable="false"
         android:gravity="right"
         android:hint="0.00"
         android:inputType="text|textNoSuggestions|numberDecimal" />

</RelativeLayout>   

1条回答
唯我独甜
2楼-- · 2019-09-07 06:25

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.

查看更多
登录 后发表回答