android 列表视图内的文本更改事件

Text change event inside listview in android

我是 android 的新手,我在列表视图中有一个 editText,当用户将一些文本或值放入 editText 时,我想要这个值应该显示在同一个 [=16= 中的文本视图中] 试图在自定义适配器的 getView 中完成它,但它不适用于 me.My 代码如下:

public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.list_style_task
                    , parent, false);
        }

        LItem p = getProduct(position);


          tvAQuantity=((TextView) view.findViewById(R.id.tvAQuantity ))  ;
        ((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
        ((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
          tvBQuantity=((TextView) view.findViewById(R.id.tvBQuantity ))  ;
        tvBQuantity.setText(p.getBQ());
          etQuantity=(EditText)view.findViewById(R.id.etTaskQuantity);
        etQuantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if(Integer.parseInt(s.toString())<=Integer.parseInt(tvBQuantity.getText().toString()))
                {
                    tvAQuantity.setText(s.toString());

                }
                else
                {

                    tvAQuantity.setText(tvBQuantity.getText());

                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    //  CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
        //cbBuy.setOnCheckedChangeListener(myCheckChangList);
    //  cbBuy.setTag(position);
    //  cbBuy.setChecked(p.selected);
        return view;
    }

需要您的宝贵建议。

改为将文本设置为 TextView,尝试更新列表模型中的字符串值。您遇到此问题是因为您的模型未在 textchange 事件上更新。

 if(Integer.parseInt(s.toString())<=Integer.parseInt(tvBQuantity.getText().toString()))
            {
                //update string value in your model.
                //tvAQuantity.setText(s.toString());

            }
            else
            {
                //update string value in your model.
                //tvAQuantity.setText(tvBQuantity.getText());

            }

2 个备选方案是:

  1. 您必须将尊重的字符串更新到您的模型 class 中,它将显示在文本视图中。
  2. 您可以在 adapter.This 中的 setText() 方法后调用 notifyDataSetChanged(); 将刷新您的列表视图。