android EditText with ListView detecting change

2019-09-06 16:15发布

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.

1条回答
Explosion°爆炸
2楼-- · 2019-09-06 16:24

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);
}
查看更多
登录 后发表回答