0

SQLITE クエリから SimpleCursorAdapter を使用して入力された TextView と Editview を持つ単純なリストビューがあります。簡単な検証を行ってデータベースを更新できるように、ユーザーがいつ EditView を離れたかを把握しようとしています。これを行うために他の投稿で提案されているいくつかの方法を試しましたが、イベントをキャッチできません。以下に、これを行うために私が試みた2つの異なる方法を示します。助けてください。大変ありがたく存じます。

    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 に答える