Android - 如何从MainActivity中的RecyclerView检索EditText值

时间:2017-04-21 12:22:19

标签: android android-edittext android-recyclerview

如何从MainActivity中的RecyclerView创建的所有EditTexts中检索值?

在我的RecyclerView适配器中,我正在扩展我的内部类:

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

我正在引用该内部类中的EditText:

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

在我想要使用的MainActivity中:

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

8 个答案:

答案 0 :(得分:9)

搞定了,这是编辑过的代码:

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

答案 1 :(得分:1)

在recyclerview适配器中的bindview方法中实现addTextChangedListener。

每次在任何单元格中修改edittext文本时,都会修改该位置的arraylist字符串。

稍后当你需要整个arraylist时,只需通过任何公共getmethod从适配器类发回它。

这应该足够了。

答案 2 :(得分:1)

试试这个:

<tree string="School Tree" editable="top">
    <field name="name"/>
    <field name="school_id"/>
    <field name="age" />
</tree>

答案 3 :(得分:1)

我在Adapter类中创建了一个getData函数。

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

然后在我的MainActivity中

public void onSave(View view) {

    String[] s=new String[length];

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

    }
}

这样,您可以在数组中保存编辑文本条目。

答案 4 :(得分:0)

这对我有用:

mySetEnabled是我在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);
    }
}

答案 5 :(得分:0)

尝试这种方式

MyPersonalViewHolder类扩展了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) {

            }
        });
    }
}

还要定义一个函数

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

现在getValue可以从MainActivity调用了。

答案 6 :(得分:0)

    go: mov ax,cs       
    mov dx,#0x4000-12   ! 0x4000 is arbitrary value >= length of
                ! bootsect + length of setup + room for stack
                ! 12 is disk parm size

! bde - changed 0xff00 to 0x4000 to use debugger at 0x6400 up (bde).  We
! wouldn't have to worry about this if we checked the top of memory.  Also
! my BIOS can be configured to put the wini drive tables in high memory
! instead of in the vector table.  The old stack might have clobbered the
! drive table.

    mov ds,ax
    mov es,ax
    mov ss,ax       ! put stack at INITSEG:0x4000-12.
    mov sp,dx
/*
 *  Many BIOS's default disk parameter tables will not 
 *  recognize multi-sector reads beyond the maximum sector number
 *  specified in the default diskette parameter tables - this may
 *  mean 7 sectors in some cases.
 *
 *  Since single sector reads are slow and out of the question,
 *  we must take care of this by creating new parameter tables
 *  (for the first disk) in RAM.  We will set the maximum sector
 *  count to 18 - the most we will encounter on an HD 1.44.  
 *
 *  High doesn't hurt.  Low does.
 *
 *  Segments are as follows: ds=es=ss=cs - INITSEG,
 *      fs = 0, gs = parameter table segment
 */

    push    #0
    pop fs
    mov bx,#0x78        ! fs:bx is parameter table address
    seg fs
    lgs si,(bx)         ! gs:si is source

    mov di,dx           ! es:di is destination
    mov cx,#6           ! copy 12 bytes
    cld

    rep
    seg gs
    movsw

    mov di,dx
    movb    4(di),*18       ! patch sector count

    seg fs
    mov (bx),di
    seg fs
    mov 2(bx),es

    mov ax,cs
    mov fs,ax
    mov gs,ax

    xor ah,ah           ! reset FDC 
    xor dl,dl
    int     0x13    

这是适配器中的代码,不能与textchange侦听器一起使用。所以我不得不使用textchange侦听器和setOnfoucusChange(100%工作)

//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

然后在活动中编写以下代码

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

答案 7 :(得分:0)

好。我有同样的问题,但是我的解决方案既不同又简单。基本上,我会列出对象,然后使用edittext更新它们的值。因此,为了正确执行此操作,我使用了for循环,而不是使用position;如果到达的对象与textview具有相同的名称,则中断循环并使用i作为索引进行更新。您可以在适配器下面看到我一直在使用的代码:

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