Search code examples
androidlistviewandroid-edittextbaseadaptertextwatcher

EditText updates items on ListView with TextWatcher


In the main class, I have a ListView and an EditText. The ListView uses a custom BaseAdapter and contains multiple items that will be displayed per row. I would like to use like a TextWatcher that will allow me to enter some number in the EditText and that input will be displayed among one of the items of the ListView.

Please see below some parts of my codes in order to explain better my question.

Main Class:

...
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 Class:

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

My aim is to enter some numbers on the EditText (e.g. 100), the number will be multiplied by the value of tvSecond, then display the final value in tvThird.

Your help will be much appreciated.


Solution

  • I hope I am understanding the situation correctly, but you can add an instance variable tvThirdText in CustomAdapter. Now, add the text watcher to the EditText etAmount and in the afterTextChanged(Editable text) method set tvThirdText to what you want and call notifyDataSetChanged() on the adapter. The last change you will have to make is in the getView() method you will have to set holder.tvThird to the correct value. Here is complete code to help you.

    Main Class:

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

    Adapter Class:

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