Jaeger
Jaeger

Reputation: 1716

Getting EditableText from EditText in RecyclerView

I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :

  1. RV_Item.xml (contains EditText).
  2. MyItem, which is an Object for RV ( contains private String Text; ).
  3. MainActivity.java, where the stuff happen.

    // My List<Object>
    List<MyItem> Items = new ArrayList<>();
    
    // For Adding, I've added FAB-Button, When Clicked, it does the following :
    Items.add(new MyItem());
    CheckForEmptyItems();
    mAdapter.notifyItemInserted(0);
    
    Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way :
    
                    for(MyItem items : Items){
                        Log.i("PrintingInfo", items.getText() );
                    }
    

The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?

Upvotes: 1

Views: 61

Answers (1)

Jaeger
Jaeger

Reputation: 1716

So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :

in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :

holder.MyEditText.addTextChangedListener(new TextWatcher() {
        @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) {
            itemList.get(position).setText(s.toString());
        }
    });

Hope that helps you!

Upvotes: 1

Related Questions