Skip to content Skip to sidebar Skip to footer

Android Edittext With Listview Detecting Change

I have a simple listview with a TextView and an Editview that is populated using the SimpleCursorAdapter from a SQLITE query. I am trying to figure out when the user has left the

Solution 1:

The view you are receiving from getView is not inflated into ListView, so your TextWatcher is not working as expected. To make it work you have to create your own adapter. For example

public class MySimpleCursorAdapter extends SimpleCursorAdapter {
    public MySimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);
    }

    @Override
    public View getView(int pos, View v, ViewGroup parent) {
        v = super.getView(pos, v, parent);
        final EditText et = (EditText) v.findViewById(R.id.classpercentage);
        et.addTextChangedListener(new TextWatcher() { 
            public void afterTextChanged(Editable s) { Log.d("TEST", "In afterTextChanged"); } 
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("TEST", "In beforeTextChanged"); } 
            public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("TEST", "In onTextChanged"); } 
        }); 
        return v;
    }
}

and then you method will be modified to this

private void showClasses(Cursor cursor) {
    SimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor, FROM, TO);
    setListAdapter(adapter);
}

Post a Comment for "Android Edittext With Listview Detecting Change"