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

时间:2015-07-30 10:07:43

标签: android expandablelistview expandablelistadapter textwatcher

From the conclusion of previous post我无法跟踪如何将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$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

1 个答案:

答案 0 :(得分:0)

自己解决了。

  1. 为ViewHolder类中的getGroupView()方法放置了所有视图[CHECKBOX,EDITTEXT,TEXTVIEW] [这只是一个包含视图的类]。

  2. 使用&#34;视图&#34;的settag()方法保存它对象,如果&#34;视图&#34;是getGroupView()方法的参数。参数返回null。

  3. 使用getTag()检索视图,如果&#34;查看&#34;则从中初始化ViewHolder对象。参数不为空。