0

我有一个简单的列表视图,其中包含一个 TextView 和一个使用 SQLITE 查询中的 SimpleCursorAdapter 填充的 Editview。我试图弄清楚用户何时离开 EditView,以便我可以进行一些简单的验证并更新数据库。我已经尝试了其他帖子中建议的几种方法来做到这一点,但我无法抓住这个事件。下面包括我尝试执行此操作的两种不同方法。请帮忙。我将不胜感激。

    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");} 
    }); 



    }
}

我在 LogCat 中没有看到任何内容,并且我在调试器中的断点没有被命中。

4

1 回答 1

0

您从中接收的视图getView未膨胀为ListView,因此您TextWatcher的工作未按预期工作。要使其工作,您必须创建自己的适配器。例如

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;
    }
}

然后你的方法将被修改为此

private void showClasses(Cursor cursor) {
    SimpleCursorAdapter adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor, FROM, TO);
    setListAdapter(adapter);
}
于 2012-06-24T22:31:29.150 回答