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 EditView so that I can do some simple validation and update the database. I've tried several ways suggested in other postings to do this but I can't catch the event. Included below are two different ways that I have tried to do this. Please help. I would greatly appreciate it.
private void showClasses(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.classrow, cursor, FROM, TO);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
//ATTEMPT 1
for (int i = 0; i < adapter.getCount(); i++){
EditText et = (EditText) adapter.getView(i, null, null).findViewById(R.id.classpercentage);
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
Log.d("TEST","In onFocusChange");
}
});
//METHOD 2
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");}
});
}
}
I am not seeing anything in LogCat and my breakpoints in the debugger aren't getting hit.
The view you are receiving from
getView
is not inflated intoListView
, so yourTextWatcher
is not working as expected. To make it work you have to create your own adapter. For exampleand then you method will be modified to this