1

I've a ListView that displays a single button and a single EditText in each ListView row.

I'm using the ViewHolder pattern in my ArrayAdapter so all the buttons share a single OnClickListener. Picking up the button click is easy because onClick(View view) in my OnClickListener gives me the view (and I use getTag() to get my model object).

I can't figure out how to have a single TextWatcher to get the changed text, because there's no view parameter in TextWatcher onTextChanged() callback. Any help appreciated!

Martin
  • 139
  • 4
  • 12

2 Answers2

1

The trick is to create a generic TextWatcher class. Then, each instance you create should be passed a reference of the View it will be placed into.

Example: https://stackoverflow.com/a/6172024/560600

Community
  • 1
  • 1
Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • Thanks - just what I was after. I ended up abandoning the EditText in ListView because of the world of pain focus problems http://stackoverflow.com/questions/2679948/focusable-edittext-inside-listview/2680077#2680077 (as Joe said "just not idiomatic-Android UI design"). For now a dialog box is good enough and I circle back on this later! – Martin Sep 05 '13 at 03:00
  • Yes! I should have remembered trying that as well. ListViews are really great for letting you display a list of memory-intensive resources, since they manage garbage collection for you. However, when I need to focus on the elements, I'm forced to use a ScrollView. – Sky Kelsey Sep 05 '13 at 18:27
0

For get the value of the EditText, in the Listener of the button i worked so:

viewHolder.button.setTag(viewHolder.YourEditText);
viewHolder.button.setOnClickListener(new OnClickListener()){
    @Override
    public void onClick(View v) {
        int p = position;
        EditText edit = (EditText) v.getTag();
        String val = edit.getEditableText().toString();
       //do what you want with the value...
    }
}

Where position is public View getView(final int position, View convertView, ViewGroup parent){...

If you'll need, i can post the entire AdapterClass.

MikeKeepsOnShine
  • 1,730
  • 4
  • 23
  • 35
  • I am having an issue with the way mine is setup. Can you provide some help... http://stackoverflow.com/questions/20524417/listview-is-blank-while-using-getfilter-function – Si8 Dec 13 '13 at 21:24