Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How To retrieve EditText value from RecyclerView in MainActivity

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");
}
like image 837
Halfacht Avatar asked Apr 21 '17 12:04

Halfacht


5 Answers

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);
}
like image 54
Rajesh Peram Avatar answered Nov 15 '22 17:11

Rajesh Peram


try this:

for(int i=0;i<adapter.getItemCount();i++){
    MyPersonalViewHolder  viewHolder= (MyPersonalViewHolder ) 
    mRecyclerView.findViewHolderForAdapterPosition(i);
    EditText editText=viewHolder.nameEditText;
}
like image 24
Firoz Jaroli Avatar answered Nov 15 '22 16:11

Firoz Jaroli


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.

like image 31
Ari Avatar answered Nov 15 '22 17:11

Ari


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.

like image 42
Ayush Bansal Avatar answered Nov 15 '22 16:11

Ayush Bansal


//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);
        }
like image 27
codingwithtashi Avatar answered Nov 15 '22 18:11

codingwithtashi