Search code examples
androidandroid-edittextexpandablelistview

How we can store edit text values inside hashmap onTextChangedListner


I am using expandable list view where each one of the child layout has one edit text. I am triggering setOnFocusChangeListener for each one of the text box's to prevent the data loss.

But I observed that the text I have entered is storing into the hashmap only when edit text cursor moves next. But I need to store the entered text while typing itself,something like onKeyUp.

Note : I have also tried Text Watchers but it is not working as expected

Code

public Map<String, String> mapping = new HashMap<>();

@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {

 ViewHolder holder;

 Map<String, String> item = (Map<String, String>) getChild(i, i1);
 String key = item.get("item_code");

 if (view == null) {
        holder = new ViewHolder();
        holder.editText = view.findViewById(R.id.txtRecordComment);
        view.setTag(holder);
 } else {
        holder = (ViewHolder) view.getTag();
 }


 holder.editText.setText(mapping.get(key));

 /*
 holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (!hasFocus) {
                mapping.put(key, holder.editText.getText().toString());
            }
        }
    }); 
   */


   holder.editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                   mapping.put(key, holder.editText.getText().toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });


    return view;
}

private class ViewHolder {
    protected EditText editText;
}

Problem with above code is if I enter text in a textbox1 of parent1 same text is getting replicated in any one of the textboxs of parent2.

It will be great help if someone suggest me how I can achive this.


Solution

  •     @Override
        public View getChildView(final int viewPosition, final int i1, boolean b, View view, ViewGroup viewGroup) {
    
         ViewHolder holder;
    
         Map<String, String> item = (Map<String, String>) getChild(i, i1);
         String key = item.get("item_code");
    
       //  if (view == null) {
                holder = new ViewHolder();
                holder.editText = view.findViewById(R.id.txtRecordComment);
      //          view.setTag(holder);
       //  } else {
       //         holder = (ViewHolder) view.getTag();
       //  }
    
    
         holder.editText.setText(mapping.get(viewPosition));
    
         /*
         holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean hasFocus) {
                    if (!hasFocus) {
                        mapping.put(viewPosition, holder.editText.getText().toString());
                    }
                }
            }); 
           */
    
    
           holder.editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
                }
    
                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                           mapping.put(viewPosition, holder.editText.getText().toString());
                }
    
                @Override
                public void afterTextChanged(Editable editable) {
    
                }
            });
    
    
            return view;
        }
    
        private class ViewHolder {
            protected EditText editText;
        }