使用按钮编辑expandablelistview的子项中的edittext文本,同时编辑子组编辑文本框

时间:2014-10-23 01:12:09

标签: android android-edittext expandablelistview android-button

我的子视图中有一个EditText字段,用于通过按钮更改的expandablelistview。布局就像这样

早餐(团体) subtractBTN EditText addBTN(子)位置0

午餐(团体) subtractBTN EditText addBTN(child)第二级的位置0

现在,每当我点击子项中的加或减按钮时,另一组中相应的子编辑文本也会发生变化。我一直在研究如何解决这个问题,但我很难过。我想我需要以某种方式使用getChild()或groupPosition,childPosition。但是使用setText()方法,我不确定在哪里放置它。

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    Item item = (Item) getChild(groupPosition, childPosition); // 10.14.14
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_view, parent, false); 
    }
    convertView.setPadding(10, 0, 0, 10);

    TextView itemNameTxt = (TextView)convertView.findViewById(R.id.itemNameTxt);
    TextView itemCostTxt = (TextView)convertView.findViewById(R.id.itemCostTxt);

    itemNameTxt.setText(item.getItem_name()); // 10.14.14
    itemCostTxt.setText(item.getItem_price());

    //add edittext for item count
    final EditText itemCount = (EditText)convertView.findViewById(R.id.itemQtTxt);
    itemCount.clearFocus(); //stop focus on edittext field 10.13.14
    // 10.11.14 ADD Buttons and each button's onClick listener and method
    //buttons and links to child_view components 
    Button addBtn = (Button)convertView.findViewById(R.id.addBTN);
    Button subBtn = (Button)convertView.findViewById(R.id.subtractBTN); //use infalInflater (convertView) as link
    //addBtn listener and method
    addBtn.setOnClickListener(new OnClickListener(){

        @SuppressLint("UseValueOf") public void onClick(View convertView) {
            // TODO Auto-generated method stub
            int sCount = Integer.parseInt(itemCount.getText().toString());// get string from edittext, turn string to int type
            if(sCount < 10){
                // Toast to test
                Toast.makeText(convertView.getContext(), "add button pressed @ \nparent " + 
                groupPosition + " & child @ "  + childPosition , Toast.LENGTH_SHORT).show();
                // set item count
                //int sCount = Integer.parseInt(itemCount.getText().toString());// get string from edittext, turn string to int type

                int intCount = sCount+1;// add one to count

                itemCount.setText(new Integer(intCount).toString());// set edittext field. getGroup(groupPosition)
                //itemCount.setText("" + intCount);
            }
        }

    });

1 个答案:

答案 0 :(得分:0)

你应该像这样使用holder / ViewHolder。

@Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {


        ChildHolder holder = new ChildHolder();

        if (convertView == null) {

            holder = new ChildHolder();
            LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_promo_items, null);

            holder.productIdView = (TextView) convertView.findViewById(R.id.promoProductId);
            holder.productNameView = (TextView) convertView.findViewById(R.id.promoProductName);
            holder.tvBudget = (TextView) convertView.findViewById(R.id.tvTeritoryBudget);
            holder.tvConfirmQty = (TextView) convertView.findViewById(R.id.tvConfirmQty);
            holder.chkGotAll = (CheckBox) convertView.findViewById(R.id.chkGetAll);


            holder.chkGotAll.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {

                    final CheckBox chkBox = (CheckBox) view;
                    PromoMaterials pMaterials=(PromoMaterials)view.getTag();
                    final TableRow tr = (TableRow) chkBox.getParent().getParent();
                    final EditText et = (EditText) tr.getChildAt(1);
                    final TextView tbudget = (TextView) tr.getChildAt(0);

                    if( chkBox.isChecked() ){
                        et.setFocusable(false);
                        et.setFocusableInTouchMode(false);
                        et.setText(tbudget.getText());

                        try{
                            pMaterials.setSelectd(true);
                        } catch(Exception e){
                            Log.e("Error Selection1", e.getMessage() + "");
                        }

                        try{

                            pMaterials.setConfirmQty( Integer.parseInt(tbudget.getText().toString()) );


                        } catch(Exception e){
                            //Toast.makeText(_context, groupPosition + ":" + childPosition, 500).show();
                        }


                    } else{
                        et.setFocusable(true);
                        et.setFocusableInTouchMode(true);
                        et.setText("");
                        try{
                            pMaterials.setSelectd(false);
                        } catch(Exception e){
                            Log.e("Error Selection2", e.getMessage() + "");
                        }
                    }
                }
            });


            CustomTextWatcher tw=new CustomTextWatcher(holder.tvConfirmQty);

            holder.tvConfirmQty.setFocusable(true);
            holder.tvConfirmQty.setFocusableInTouchMode(true);

            holder.tvConfirmQty.addTextChangedListener(tw);

            convertView.setTag(holder);
        } else{
            holder = (ChildHolder) convertView.getTag();
        }

        PromoMaterials pMaterials = DoctorVisit.GLOBAL_CHILD_DATA_LIST.get(_listDataHeader.get(groupPosition)).get(childPosition);

        holder.productIdView.setText(pMaterials.getProductId()+"");
        holder.productNameView.setText(pMaterials.getSubCategoryName());
        holder.tvBudget.setText(pMaterials.getTeritoryBudget() + "");
        holder.chkGotAll.setTag(pMaterials);
        holder.tvConfirmQty.setTag(pMaterials);

        if( pMaterials.isSelectd() ){

            holder.chkGotAll.setChecked(true);
            //holder.tvConfirmQty.setText( pMaterials.getConfirmQty() + "" );
        } else{
            holder.chkGotAll.setChecked(false);
            //holder.tvConfirmQty.setText("");
        }  

        holder.tvConfirmQty.setText( pMaterials.getConfirmQty() + "" );

        return convertView;
    }

<强> ChildHolder:

static class ChildHolder{
        TextView productIdView;
        TextView productNameView;
        TextView tvBudget;
        TextView tvConfirmQty;
        CheckBox chkGotAll;
    }

Here full example。您可以下载并按照示例进行操作。