Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在textwatcher中检索回收器视图项目位置?_Android_Android Recyclerview - Fatal编程技术网

Android 如何在textwatcher中检索回收器视图项目位置?

Android 如何在textwatcher中检索回收器视图项目位置?,android,android-recyclerview,Android,Android Recyclerview,在检索CustomWatcher中的位置时,我获得了NullPoniteRexeption。 我的CustomTextWatcher类: public static class CustomWatcher implements TextWatcher { private MyViewHolder hol; private EditText editText; public CustomWatcher(EditText editText) {

在检索CustomWatcher中的位置时,我获得了NullPoniteRexeption。 我的CustomTextWatcher类:

public static class CustomWatcher implements TextWatcher {

        private MyViewHolder hol;
        private EditText editText;

        public CustomWatcher(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int position = (int)editText.getTag(R.id.id_ans_text);
            Log.d("SUB", position+" "+s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }

    }
holder实现类,我在其中设置了观察者:

 public class MyViewHolder extends RecyclerView.ViewHolder{

        public EditText answer;
        private int position;

        public MyViewHolder(View view) {
            super(view);
            context = view.getContext();
            answer = (EditText) view.findViewById(R.id.id_ans_text);
            Log.d("ss",answer+"");
            CustomWatcher textWatcher = new CustomWatcher(answer);
            answer.addTextChangedListener(textWatcher);
}
}
在onBindViewHolder中:

 headerHolder.answer.setTag(R.id.id_ans_text, position);

因为在RecyclerView中,每个项目都有一个单独的CustomWatcher实例,所以只需将该位置保留在实例变量中即可

首先,修改CustomWatcher,使其将位置作为构造函数的参数:

public static class CustomWatcher implements TextWatcher {

        private MyViewHolder hol;
        private EditText editText;
        private int position;

        public CustomWatcher(EditText editText, int pos) {
            this.editText = editText;
            this.position = pos;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //int position = (int)editText.getTag(R.id.id_ans_text);
            Log.d("SUB", position+" "+s);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
}
然后,创建CustomWatcher实例,并在
onBindViewHolder()
覆盖中调用
addTextChangedListener()

@Override
public void onBindViewHolder(ViewHolder headerHolder, int position) {

    CustomWatcher textWatcher = new CustomWatcher(headerHolder.answer, position);
    headerHolder.answer.addTextChangedListener(textWatcher);

}