EditText使用TextWatcher

时间:2016-06-12 22:50:24

标签: android listview android-edittext baseadapter textwatcher

在主类中,我有一个ListView和一个EditText。 ListView使用自定义BaseAdapter并包含将在每行显示的多个项目。 我想使用TextWatcher,它允许我在EditText中输入一些数字,并且该输入将显示在ListView的一个项目中。

请在下面查看我的代码的某些部分,以便更好地解释我的问题。

主类:

...
etAmount = (EditText) findViewById(R.id.edt_text);
lvDisplay = (ListView) findViewById(R.id.listView_display);
...
CustomAdapter adapter = new CustomAdapter(this, menuList, new CustomAdapter.onDoneClick() {
    @Override
    public void onClick(View v, int position) {}
    });
lvDisplay.setAdapter(adapter);
...

CustomAdapter类:

...
public CustomAdapter(Context context, List<MenuModel> objects, onDoneClick listener) {
    this.context = context;
    this.menuList = objects;
    this.mListener = listener;
}

public interface onDoneClick{
    void onClick(View v, int position);
}

@Override
public int getCount() {
    return menuList.size();
}

@Override
public Object getItem(int position) {
    return menuList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    View view = convertView;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (view  == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.item_layout, parent, false);
        holder.tvFirst = (TextView) view.findViewById(R.id.item1);
        holder.tvSecond = (TextView) view.findViewById(R.id.item2);
        holder.tvThird = (TextView) view.findViewById(R.id.item3);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    final MenuModel item = menuList.get(position);

    if (item != null) {
        holder.tvFirst.setText(item.getName());
        holder.tvSecond.setText(item.getRate());
    }

    return view;
}

static class ViewHolder {
    TextView tvFirst;
    TextView tvSecond;
    TextView tvThird;
}

我的目标是在EditText上输入一些数字(例如100),数字将乘以tvSecond的值,然后在tvThird中显示最终值。

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我希望我能正确理解这种情况,但您可以在tvThirdText中添加实例变量CustomAdapter。现在,将文本观察器添加到EditText etAmountafterTextChanged(Editable text)方法集tvThirdText中,以便在适配器上调用notifyDataSetChanged()。您需要进行的最后一项更改是使用getView()方法,您必须将holder.tvThird设置为正确的值。这是完整的代码可以帮助您。

主类:

        etAmount.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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            adapter.tvThirdText = Integer.parseInt(s.toString().trim());
            adapter.notifyDataSetChanged();
        }
    });

适配器类:

public int tvThirdText; //can make this private and add setter method if needed
...

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
    if (item != null) {
        holder.tvFirst.setText(item.getName());
        holder.tvSecond.setText(item.getRate());
        holder.tvThird.setText(tvThirdText * item.getRate());
    }