多次调用 EditText TextChangeListener

EditText TextChangeListener called multiple times

在此特定场景中,ExpandableList 假设将 2 个列表显示为组。当用户选中该复选框时,它会将值添加到 EditText,这就是 TextChangeListener 在第一次输入时启动 3 次,在第二次输入时启动 1 次的位置,如输出部分所示。

检查更改时 EditText 收到的输入是

22448 for the 0 item in the arraylist

&

56.12 for the 1st item in the arraylist

代码

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;          
     }   

}

输出:

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@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@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@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@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@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@52882534
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@52a697d0
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@528a1cfc
07-30 04:06:47.666    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@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@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@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@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@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@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@52882534
07-30 04:06:58.826    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@52a697d0
07-30 04:06:58.826    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@528a1cfc
07-30 04:06:58.830    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@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@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@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@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@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@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@52882534
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter@52a697d0
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter@528a1cfc
07-30 04:06:58.834    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter@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@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@52bd946c
07-30 04:07:03.337    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@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@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@52bd946c
07-30 04:07:06.057    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0: Some text this: com.example.ExpandableListAdapter@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@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@52bd946c
07-30 04:07:06.085    1432-1432/? I/System.out﹕ AFTER TEXT CHANGE >>> arg0:  this: com.example.ExpandableListAdapter@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. 将 getGroupView() 方法的所有视图[CHECKBOX、EDITTEXT、TEXTVIEW] 放置在 ViewHolder class 中 [这只是一个包含视图的 class。

  2. 使用 "view" 对象的 settag() 方法保存它,如果 "view" 参数返回 null,它是 getGroupView() 方法的参数。

  3. 如果 "view" 参数不为空,则使用 getTag() 检索视图并从中初始化 ViewHolder 对象。