1

I have a recyclerview that load up to 30 itemviews and more In every itemview it has an Edittext to let the user enters some data And a save button. Inside the save button onclick

for (i in 0 until count){ //count = myRecycler.layoutmanager.itemcount
val view = myRecycler.layoutmanager!!..findViewByPosition(i)
val input: EditText = view!!.findViewById(R.id.input)
list.add(input.text.toString())
} 

It goes for every itemview by position But because recyclerview just load the items that fits the screen it gives null pointer at the view val for the items that are out the screen Is there any solution to make this happen A solution i got Is to add A save button on every itemview so that when the user enters data he clicks the save button and so on. But that's not what I need

3 Answers3

1

I would suggest that you fire a callback on each editText's lost focus via setOnFocusChangeListener to update the data list in Activity or Fragment you have. The code in RecyclerView.Adapter

interface Calllback{
    function updateValue(int position, String value)
}

@Override
public void onBindViewHolder(myViewHolder holder, int position) {

    ...
    holder.editText.setOnFocusChangeListener(new OnFocusChangeListener() {          
        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus) {
                callback.updateValue(holder.getAdapterPosition(), ((EditText)v).getText().toString());
            }
        }
    });
    ...
}

and in activity which implements the Callback interface,

class MainActivity extends AppCompatActivity implements Callback{

    private String data = new String[30];

    ...

    @Override
    function updateValue(int position, String value){
        data[position] = value;
    }

    ... 
}
hjchin
  • 864
  • 2
  • 8
  • 25
  • why use `holder.getPosition()` while there is `int position` parameter is the same or not, because `holder.getPosition()` is deprecated – Bashar ALkaddah Aug 27 '18 at 13:07
  • when I use the Callback in Fragment it gives me a kotlin.KotlinNullPointerException at callback in the RecyclerAdapter – Bashar ALkaddah Aug 27 '18 at 15:23
  • oops, my mistake. it supposed to be getAdapterPosition() and the reason to use it over position is because if the data has been changed, adapter might not be updated. More info here, https://medium.com/@haydar_ai/better-way-to-get-the-item-position-in-androids-recyclerview-820667d435d4. – hjchin Aug 28 '18 at 01:58
  • if you use Fragment, you need to pass the Callback instance (the fragment itself) to Adapter like here https://stackoverflow.com/a/39715251/1177865 – hjchin Aug 28 '18 at 02:29
1

But that's not what i need well whether you need it or not is irrelevant.
That's how a RecyclerView works.
The value entered in one item's EditText must be saved in the adapter and then you must call notifyItemChanged() else it will be lost after the scrolling.
This applies to every change you make in a RecyclerView item.
If you save all the changes in the adapter then you can loop through the adapter items and not directly through the RecyclerView items and this will not cause any errors.

0

Use TextWatcher on every EditText and get callbacks whenever their text got changed and save the text as follows:

editText.addTextChangedListener(object : TextWatcher
        {
            override fun afterTextChanged(s: Editable?) {
               //Save your string here

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
Karan Kalsi
  • 789
  • 3
  • 21
  • IMO, be careful of the addTextChangedListener, don't add it multiple times in the onBindViewHolder function and result in multiple fire of this event. – hjchin Aug 27 '18 at 09:51
  • You need to addTextChangedListener only in the ViewHolder constructor and when you get callbacks you can use getAdapterPosition() for getting the position of item for which we got callback. – Karan Kalsi Aug 27 '18 at 13:22
  • your method caused me multiple times of data for example i have entered 30 data but the stored data could reach 60 or 90 – Bashar ALkaddah Aug 27 '18 at 15:36