Android - How To retrieve EditText value from RecyclerView in MainActivity

23,737

Solution 1

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);
}

Solution 2

try this:

for(int i=0;i<adapter.getItemCount();i++){
    MyPersonalViewHolder  viewHolder= (MyPersonalViewHolder ) 
    mRecyclerView.findViewHolderForAdapterPosition(i);
    EditText editText=viewHolder.nameEditText;
}

Solution 3

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.

Solution 4

//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);
        }

Solution 5

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.

Share:
23,737
Halfacht
Author by

Halfacht

"Hope is the currency of change"

Updated on July 09, 2022

Comments

  • Halfacht
    Halfacht almost 2 years

    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");
    }
    
  • Vygintas B
    Vygintas B about 7 years
    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
  • Ari
    Ari about 7 years
    Yeah that should also work, and will give better performance.
  • Halfacht
    Halfacht about 7 years
    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.
  • Ari
    Ari about 7 years
    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.
  • Halfacht
    Halfacht about 7 years
    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
    Halfacht about 7 years
    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.
  • Ganesh K
    Ganesh K almost 4 years
    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);