w.donahue
w.donahue

Reputation: 10886

Android EditText within a ListView

I have created a custom Array Adapter to bind a custom row that contains some static text and an editable EditText. I am trying to register to be notified when the user changes the text within the edit text and when notified to determine which ArrayList row the modified EditText corresponds to.

In the past with other types of views such as a Spinner I could simply put a reference to the parent view and the row number into the tag for the Spinner view. And then when I was notified that the value changed I read the tag to determine how to correlate it back to the master ArrayList.

The problem with registering to be notifed with an EditText change is that you do not get back a view but instead get a TextWatcher and I have no way to correlate back to the parent view or ArrayList row.

What is the technique that you need to use in this circumstance?

Upvotes: 0

Views: 2004

Answers (1)

EricLarch
EricLarch

Reputation: 5773

You can use onEditorAction on your EditText in your ArrayAdapter:

mEditText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        // Parse parent tree to find position of view
        int position = 0;
        View v = null;
        while (view != v && position < mListView.getChildCount())
            v = mListView.getChildAt(position++);
        // do something
        something(position);
        // do not consume the action
        return false;
}
});

Note that using this method, you are going to trigger an event only when user press "ok", "enter", "done", etc. on the keyboard.

Upvotes: 2

Related Questions