Saving the values entered into EditTexts of a RecyclerView
Asked Answered
P

3

5

I have a RecyclerView where each row also has an EditText that allows the user to enter a value.

However when the user rotates the screen the values reset to their default blank values.

So I wanted to save the values in a List or Map so I can refill the list on restore.

However I don't know how to "iterate over the current list" and grab the EditTexts and extract the values.

Preterition answered 26/4, 2016 at 18:25 Comment(2)
depends on how many edit texts you have? if few then you should try to save there values in onSavedInstanceState and retrieve it in onCreateRomany
you might want to check this question along with solutions: #31844873Prohibitory
D
9

If you take for example the following RecyclerView.Adapter implementation, you'll notice a few things. The List containing the data is static meaning it will not be reset on orientation changes. We are adding a TextWatcher to the EditText to allow us to update the values when they are modified, and also we are keeping track of the position by adding a tag to the view. Note that this is my particular approach, there are many ways of solving this.

Demo

enter image description here

Adapter

public class SampleAdapter extends RecyclerView.Adapter{
    private static List<String> mEditTextValues = new ArrayList<>();

    public SampleAdapter(){
        //Mocking content for the editText
        for(int i=1;i<=10;i++){
            mEditTextValues.add("I'm editText number "+i);
        }
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CustomViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.edittext,parent,false));
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        CustomViewHolder viewHolder = ((CustomViewHolder)holder);
        viewHolder.mEditText.setTag(position);
        viewHolder.mEditText.setText(mEditTextValues.get(position));
    }
    @Override
    public int getItemCount() {
        return mEditTextValues.size();
    }

    public class CustomViewHolder extends RecyclerView.ViewHolder{
        private EditText mEditText;
        public CustomViewHolder(View itemView) {
            super(itemView);
            mEditText = (EditText)itemView.findViewById(R.id.editText);
            mEditText.addTextChangedListener(new TextWatcher() {
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
                public void afterTextChanged(Editable editable) {}
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if(mEditText.getTag()!=null){
                        mEditTextValues.set((int)mEditText.getTag(),charSequence.toString());
                    }
                }
            });
        }
    }
}

(gif may look like it's being reset, since it's in a loop, it's not)

Dugger answered 26/4, 2016 at 19:27 Comment(0)
A
1

This is issue for RecyclerView recycle. Just override these function:

@Override
public long getItemId(int position) {
    return position;
} 

@Override public int getItemViewType(int position) {
    return position;
}
Arvell answered 12/2, 2021 at 7:40 Comment(0)
C
0

Simple add this into your adapter class for showing editext data when scroll into recyclerview

@Override
public int getItemViewType(int position) {
    return position;
}
Cummins answered 17/7 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.