Android - How To retrieve EditText value from Recy

2019-05-12 03:21发布

问题:

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

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


回答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.



回答3:

try this:

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


回答4:

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.



回答5:

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


回答6:

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.



回答7:

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