多个 TextWatcher 添加到 ExpandableListView 的组视图中的同一个 EditText

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

我无法跟踪如何将 TextWatcher 多次添加到 ExpandableListView 中的 EditText。 ArrayList 只有 2 个元素。

输出应该只列出 0 和 1 一次,但它被调用了两次。

适配器:

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

}

输出:

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

自己解决了。

  1. 将 getGroupView() 方法的所有视图[CHECKBOX、EDITTEXT、TEXTVIEW] 放置在 ViewHolder class 中 [这只是一个包含视图的 class。

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

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