How to get the Edit text position from Recycler View adapter using Text Watcher in android

后端 未结 2 435
不知归路
不知归路 2021-01-06 08:25

I want to know how to get position of Edit Text from Recycler View adapter.I used Card View with in that horizontal Linear Layout has three view TextView,EditText view and

相关标签:
2条回答
  • 2021-01-06 08:48

    In onBindViewHolder method of your Adapter, set tag for your EditText like this:

    holder.editText.setTag(position);
    

    And in your ViewHolder, add TextWatcher to your EditText

    public static class ViewHolder extends RecyclerView.ViewHolder {
    
        EditText editText ;
    
        public ViewHolder(View itemView) {
            super(itemView);
            editText = itemView.findViewById(R.id.editText);
            MyTextWatcher textWatcher = new MyTextWatcher(editText);
            editText.addTextChangedListener(textWatcher);
        }
    }
    

    And here is your TextWatcher:

    public class MyTextWatcher implements TextWatcher {
        private EditText editText;
    
        public MyTextWatcher(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();
            // Do whatever you want with position
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    }
    

    NOTE: Make sure you call setTag method from your EditText before calling setText method, else it will throw NullPointerException. Or alternatively you can add null check when calling getTag method.

    UPDATE 1

    If your EditText already has another tag set, use ID to identify tags. e.g. when setting tag use this:

    holder.editText.setTag(R.id.editText, position);
    

    where R.id.editText is valid id of any of your resources (See documentation for details).

    And also when getting value:

    int position = (int) editText.getTag(R.id.editText);
    
    0 讨论(0)
  • 2021-01-06 09:10

    Assume you use the following CustomViewHolder

    public static class CustomViewHolder extends RecyclerView.ViewHolder {
        private TextView textView; 
        private TextView textView2;
        private EditText editText;
        private int position;
        public CustomViewHolder (View view) {
            super(view);
            textView = (TextView) view.findViewById(R.id.text_view);
            textView2 = (TextView) view.findViewById(R.id.text_view2);
            editText = (EditText) view.findViewById(R.id.edit_text);
            editText.addTextChangedListener(new CustomWatcher(this));
        }
    }
    

    and use the following CustomWatcher

    public static class CustomWatcher implements TextWatcher {
        private int CustomViewHolder holder;
        public CustomWatcher(CustomViewHolder holder){
            this.holder = holder;
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    
        @Override
        public void afterTextChanged(Editable s) {
    
        }
    
        public int getPosition(){
            return holder.position;
        }
    }
    

    Then, in your onBindViewHolder

    @Override
    public void onBindViewHolder(final CustomViewHolder holder, final int position) {
        holder.position = position;
    }
    
    0 讨论(0)
提交回复
热议问题