当前位置:  开发笔记 > Android > 正文

Recyclerview中的多个edittext

如何解决《Recyclerview中的多个edittext》经验,为你挑选了1个好方法。

我们正在开发一款可以分享团体成本的应用.为此,您可以选择与您共享下一个账单的人.我制作了一种"list-view-item",其中包含每个用户的"借记"和"信用"字段,并根据共享付款的人数将这些项目添加到Recyclerview.但是,现在我正在尝试读取在这些edittexts中输入的所有值,但我不知道如何.在recyclerview中生成列表的代码如下:

private void initRecyclerView() {
    rvTransactions = (RecyclerView) findViewById(R.id.rvTransactions);
    lmTransactions = new LinearLayoutManager(this);
    rvTransactions.setLayoutManager(lmTransactions);
    transactionAdapter = new RecyclerView.Adapter() {
        @Override
        public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_transaction, viewGroup, false);
            return new CustomViewHolder(view);
        }

        @Override
        public void onBindViewHolder(CustomViewHolder viewHolder, int i) {
            viewHolder.noticeSubject.setText(participatingUsers.get(i).getFirstname() + " " + participatingUsers.get(i).getLastname());
        }

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

    };
    rvTransactions.setAdapter(transactionAdapter);
}

private class CustomViewHolder extends RecyclerView.ViewHolder {

    private TextView noticeSubject;
    private EditText userDebt,userCredit;

    public CustomViewHolder(View itemView) {
        super(itemView);

        noticeSubject = (TextView) itemView.findViewById(R.id.tvTransactionUser);
        userCredit = (EditText) findViewById(R.id.etTransactionPayed);
        userDebt = (EditText) findViewById(R.id.etTransactionOwes);
    }
}

participantUsers对象是用户列表.用户具有以下字段:

private int id;
private boolean isSelected;
private String firstname, lastname, email, password, api_key, creditcardnumber;

这就是视图的样子,您可以看到动态添加的

我希望能够为所有用户获取在edittexts中输入的值"payed"和"owes".

任何帮助将非常感激.



1> kandroidj..:

循环遍历RecyclerView的childCount并检查以确保您的读取项类型为CustomViewHolder,强制转换为该类型并读取EditText值.就像是:

 public void printAllEditTextValues(){
     int childCount = rvTransactions.getChildCount(); 
     for(int i = 0; i < childCount; i++){
         if(rvTransactions.findViewHolderForLayoutPosition(i) instanceOf CustomViewHolder){
           CustomViewHolder childHolder = (CustomViewHolder) rvTransactions.findViewHolderForLayoutPosition(i);

           Log.d("EDIT_TEXT_1" , childHolder.userCredit.getText().toString());
           Log.d("EDIT_TEXT_2" , childHolder.userDebt.getText().toString());
         }
     }

你的适配器缺少你的编辑文本itemView.findViewById(int),使用:

private class CustomViewHolder extends RecyclerView.ViewHolder {

    private TextView noticeSubject;
    private EditText userDebt,userCredit;

    public CustomViewHolder(View itemView) {
        super(itemView);

        noticeSubject = (TextView) itemView.findViewById(R.id.tvTransactionUser);
        userCredit = (EditText) itemView.findViewById(R.id.etTransactionPayed);
        userDebt = (EditText) itemView.findViewById(R.id.etTransactionOwes);
    }
}

希望这有帮助,祝你好运,快乐编码!

推荐阅读
mobiledu2402851203
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有