我们如何在Android中的RecylerView卡中获取并存储editText值?

时间:2017-04-29 06:25:09

标签: android android-recyclerview recycler-adapter

请给我一个解决方案我是Android新手,On BindView Holder Code是:

我在RecylerView中使用卡片

@Override
    public void onBindViewHolder(SeedlingViewHolder holder, final int position) {

        holder.seedling_CardView.setTag(position);

        if(holder instanceof SeedlingViewHolder) {
            holder.centerUnit.setText("Assigned To : "+placedOrderDetailBeanArray.get(position).eoCenterUnit);
            holder.creationDate.setText("Creation Date :"+placedOrderDetailBeanArray.get(position).orderCreationDate);
            holder.no_bag.setText("No. of Bag :"+placedOrderDetailBeanArray.get(position).noOfBags);
            holder.no_seedling.setText("No. of Seedling :"+placedOrderDetailBeanArray.get(position).noOfSeedling);

          /*  holder.target.setTag(position);
             value=(String) holder.centerUnit.getTag(R.id.seedling_Target_GM);*/

            holder.save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    placedOrderDetailBeanArray.get(position).target = targetVal;
                    Log.v("ORDER DEATIL ***** ",placedOrderDetailBeanArray.toString());
                }
            });
            Log.v("Seeding Adapter","Holder");
        }


    }

我的编辑文本名称是目标,我想将此值保存在变量中,以便我可以在服务器上发送它

@Override
    public int getItemCount() {
        return placedOrderDetailBeanArray.size();
    }

    public class SeedlingViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView creationDate,completionDate,no_seedling,no_bag,achievement,centerUnit;
        public EditText target;
        public Button save;
        public View seedling_CardView;
        public SeedlingViewHolder(View convertView) {
            super(convertView);
            seedling_CardView = (CardView) convertView.findViewById(R.id.cardViewSeedling_GM);
            seedling_CardView.setOnClickListener(this);

            creationDate=(TextView)convertView.findViewById(R.id.seedling_CreationDate_GM);
            no_seedling=(TextView)convertView.findViewById(R.id.seedling_no_seedling_GM);
            no_bag=(TextView)convertView.findViewById(R.id.seedling_bag_GM);
            centerUnit=(TextView)convertView.findViewById(R.id.seedlingCenterUnit_GM);
            save=(Button)convertView.findViewById(R.id.btnSave_GM);

            target=(EditText)convertView.findViewById(R.id.seedling_Target_GM);
        }

        @Override
        public void onClick(View v) {

        }
    }

2 个答案:

答案 0 :(得分:0)

首先:使用带有所需变量的setter和getter方法创建自定义包装器类

喜欢这个:

public class WrapperClass {

    private String Name;
    private String MaidID;
    private String Phone;

    public WrapperClass(String name,String Phone,String Mail) {
        Name = name;
        this.Phone=Phone;
        MaidID=Mail;

    }


    public String getMaidID() {
        return MaidID;
    }

    public void setMaidID(String maidID) {
        MaidID = maidID;
    }

// Other Variable Getter Setter with constructor of values you want to retrieve

第二: 在主要活动上创建自定义列表,您可以在其中编辑文本

List<WrapperClass> Data=new ArrayList<WrapperClass>();

然后调用这样的构造函数

WrapperClass wrap=new WrapperClass(nameEdittext.gettext().toString,MailEdittext.gettext().toString,PhoneEdittext.gettext().toString);

然后将WrapperClass对象添加到列表中

data.add(wrap)

最后在onBindView Holder中得到像这样的值

    wrapperClass = UsersList.get(position);
holder.Name.setText(wrapperClass.getName());

注意:不要忘记将自定义列表作为recyclerAdapter的参数传递。

答案 1 :(得分:0)

您可能需要在列表或数组中保存字符串(与列表大小相同)另外,在模型中使用一个字段可以在edittext更改侦听器上保存值,然后在需要时从列表中获取值

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private String[] mDataset;

public MyAdapter(String[] myDataset) {
    mDataset = myDataset;
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_edittext, parent, false);
    // pass MyCustomEditTextListener to viewholder in onCreateViewHolder
    // so that we don't have to do this expensive allocation in onBindViewHolder
    ViewHolder vh = new ViewHolder(v, new MyCustomEditTextListener());

    return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    // update MyCustomEditTextListener every time we bind a new item
    // so that it knows what item in mDataset to update
    holder.myCustomEditTextListener.updatePosition(holder.getAdapterPosition());
    holder.mEditText.setText(mDataset[holder.getAdapterPosition()]);
}

@Override
public int getItemCount() {
    return mDataset.length;
}


public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public EditText mEditText;
    public MyCustomEditTextListener myCustomEditTextListener;

    public ViewHolder(View v, MyCustomEditTextListener myCustomEditTextListener) {
        super(v);

        this.mEditText = (EditText) v.findViewById(R.id.editText);
        this.myCustomEditTextListener = myCustomEditTextListener;
        this.mEditText.addTextChangedListener(myCustomEditTextListener);
    }
}

// we make TextWatcher to be aware of the position it currently works with
// this way, once a new item is attached in onBindViewHolder, it will
// update current position MyCustomEditTextListener, reference to which is kept by ViewHolder
private class MyCustomEditTextListener implements TextWatcher {
    private int position;

    public void updatePosition(int position) {
        this.position = position;
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        // no op
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        mDataset[position] = charSequence.toString();
    }

    @Override
    public void afterTextChanged(Editable editable) {
        // no op
    }
}
}