7

How can I retrieve the value from all EditTexts created by the RecyclerView in MainActivity?

In my RecyclerView Adapter I'm extending my inner class:

public class MyPersonalAdapter extends RecyclerView.Adapter<MyPersonalAdapter.MyPersonalViewHolder>

I'm getting a reference to the EditText in that inner class:

 class MyPersonalViewHolder extends RecyclerView.ViewHolder {

        TextView numberTextView;
        EditText nameEditText;

        public MyPersonalViewHolder(View itemView) {
            super(itemView);
            numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
            nameEditText = (EditText) itemView.findViewById(R.id.et_name);
        }
    }

and in my MainActivity I want to use:

for (int i = 0; i < count; i++) {
    String name = "Somehow get that name";
    cv.put(MyContract.MyEntry.COLUMN_NAME, "name");
}
Halfacht
  • 924
  • 1
  • 12
  • 22

8 Answers8

13

Got it working, here is the edited code:

mAdapter = new MyClassAdapter(this, mDataset.size);
mRecyclerView.setAdapter(mAdapter);
mRecyclerview.setItemViewCacheSize(mDataset.size());

List<ContentValues> list = new ArrayList<>();

for (int i = 0; i < mDataset.size(); i++) {
    View view = recyclerView.getChildAt(i);
    EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
    String name = nameEditText.getText().toString();

    ContentValues cv = new ContentValues();
    cv.put(MyContract.MyEntry.COLUMN_NAME, name);
    list.add(cv)
}

// I encapsulated this in a try catch
for (ContentValues c:list) {
    mDb.insert(MyClassContract.MyClassEntry.TABLE_NAME, null, c);
}
Halfacht
  • 924
  • 1
  • 12
  • 22
Rajesh Peram
  • 1,128
  • 8
  • 10
  • 6
    I thought this code works. but it only works for a ListView. Because if the system recycles the Views the app will crash giving a null pointer exeption. So this works as long as all EditTexts fits on the screen. – Halfacht Apr 22 '17 at 11:44
  • 2
    As suggested by @Halfacht , this solution does not work for recyclerview since the views get recycled and null pointer exception comes when we try to call recyclerview.getChildAt(i); – Ganesh K May 31 '20 at 10:22
2

Implement a addTextChangedListener inside bindview method in the recyclerview adapter.

everytime the edittext text is modified in any cell modify the arraylist string at that position.

And later when you need the whole arraylist, just send it back from the adapter class via any public getmethod.

This should be enough.

Ari
  • 1,296
  • 1
  • 18
  • 36
  • 2
    I would not recommend implementing TextChangeListener inside bindview method as It's quite expensive method performence vise and scrolling would be lagging. Better implement it in ViewHolder – Vygintas B Apr 21 '17 at 12:31
  • Yeah that should also work, and will give better performance. – Ari Apr 21 '17 at 12:33
  • Implementing an addTextChangedListener to update an arraylist seems like a lot of calculation. Because the value is only needed when the word (or sentence) is done. – Halfacht Apr 21 '17 at 12:40
  • Use the method aftertextchanged instead of ontextchanged. But i can't think of a different way to get all the cell edittext fields without implementing a textchangelistener. – Ari Apr 21 '17 at 12:51
2

try this:

for(int i=0;i<adapter.getItemCount();i++){
    MyPersonalViewHolder  viewHolder= (MyPersonalViewHolder ) 
    mRecyclerView.findViewHolderForAdapterPosition(i);
    EditText editText=viewHolder.nameEditText;
}
Firoz Jaroli
  • 505
  • 5
  • 11
  • Too bad this does not work when the RecyclerView recycles some views. Side node: for this to work the inner class MyPersonalViewHolder must be public and also the instance variable: public EditText nameEditText. The inner class must be referenced by: MyPersonalAdapter.MyPersonalViewholder. Or the adapter class should be imported. – Halfacht Apr 22 '17 at 12:04
1
//So the other day I spend full day to get data(list of edittext) from recyclerview to activity when i press 
button in activity
//perform onclick of button

Here is the code in adapter,Did't work with textchange listener..So i had to used textchange listener and setOnfoucusChange(100% working)

    holder.mComment.setOnFocusChangeListener(new 
    View.OnFocusChangeListener() {

            @Override
        public void onFocusChange(View v, boolean hasFocus) {

            /* When focus is lost check that the text field
             * has valid values.
             */

            if (!hasFocus) {
                String data=holder.mComment.getText().toString();
                commentList[position]=data;
            }

            if(position==mList.size()-1){
                holder.mComment.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                        commentList[position]=s.toString();
                    }

                    @Override
                    public void afterTextChanged(Editable editable) {

                    }
                });
            }
        }
    });
Intent intent = new Intent("mrn_intent");
            Bundle args = new Bundle();
            args.putSerializable("comment_list",(Serializable)commentList);
            args.putSerializable("rating_list", (Serializable) mRatingList);
            intent.putExtra("BUNDLE_COMMENT",args);

LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

And in activity write the following code

Bundle args2 = intent.getBundleExtra("BUNDLE_COMMENT");
        if(args2!=null){

            list = (String[]) args2.getSerializable("comment_list");

            Log.d(TAG, "onReceive: list+++=>>>>>>"+list);
        }
Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30
0

This worked for me:

mySetEnabled is a method I implemented within my viewHolder.

if(mRecView!=null) {
    int size=mRecView.getChildCount();
    for (int i = 0; i < size; i++) {
        myAdapter.myViewHolder wordView = (myAdapter.myViewHolder)mRecView.findViewHolderForLayoutPosition(i);
        if(wordView!=null)
            wordView.mySetEnabled(state);
    }
}
Banana
  • 2,435
  • 7
  • 34
  • 60
Sharone Lev
  • 791
  • 7
  • 15
0

I created a getData function inside my Adapter class.

public String getData()
{
    String s;
    s=txt1.getText().toString();
    return s;
}

Then in my MainActivity

public void onSave(View view) {

    String[] s=new String[length];

    for(int i=0;i<ad.getItemCount();i++)
    {
       s[i]=ad.getData().toString();

    }
}

By this, you can save edit text entries in the array.

Banana
  • 2,435
  • 7
  • 34
  • 60
-1

Try this way,

class MyPersonalViewHolder extends RecyclerView.ViewHolder {

    TextView numberTextView;
    EditText nameEditText;

    public MyPersonalViewHolder(View itemView) {
        super(itemView);
        numberTextView = (TextView) itemView.findViewById(R.id.tv_number);
        nameEditText = (EditText) itemView.findViewById(R.id.et_name);
        nameEditText.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) {
                values.add(getAdapterPosition(),s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}

Also, define a function

public String getValue(int position){
    return  values.get(position);
}

Now getValue can call from MainActivity.

Abhishek c
  • 539
  • 6
  • 16
-1

OK. I had the same problem, but my solution was different and simpler. Basically, I'd a list of objects, and I was using the edittext to update their values. So, to do it correctly, instead of using position, I used a for loop and if I reach the object that have the same name of my textview, I break the loop and update using i as my index. You can see the code that I have been using in my adapter bellow:

 int i;
                    for(i = 0; i<list.size(); i++){
                        if(list.get(i).getName().equals(holder.name.getText())){
                            break;
                        }
                    }

                    Commodity updated = list.get(i);

                    updated.setValor(Float.parseFloat(s.toString())); // recovering value of my edit text
                    list.set(i, updated);
                    atualizado[i] = Float.parseFloat(s.toString());