简体   繁体   English

从 RecyclerView 中的 EditText 获取 EditableText

[英]Getting EditableText from EditText in RecyclerView

I've recycler view with 0 items, I've added an option to manually add the items, my stracture is simple :我有 0 个项目的回收者视图,我添加了一个手动添加项目的选项,我的结构很简单:

  1. RV_Item.xml (contains EditText). RV_Item.xml(包含 EditText)。
  2. MyItem, which is an Object for RV ( contains private String Text; ). MyItem,它是 RV 的对象(包含私有字符串文本;)。
  3. MainActivity.java, where the stuff happen. MainActivity.java,事情发生的地方。

     // My List<Object> List<MyItem> Items = new ArrayList<>(); // For Adding, I've added FAB-Button, When Clicked, it does the following : Items.add(new MyItem()); CheckForEmptyItems(); mAdapter.notifyItemInserted(0); Now, When the user click the save button, i want to take all the edittext in all the items he had added, i'm doing in the following way : for(MyItem items : Items){ Log.i("PrintingInfo", items.getText() ); }

The problem is, i'm not getting the text he entered in all EditText fields, and it's returning Null in all of them, What's the issue in this ?问题是,我没有得到他在所有 EditText 字段中输入的文本,并且在所有字段中都返回 Null,这有什么问题?

So, i don't know why always i know the answer after posting, but here's how you gonna know what the user typed :所以,我不知道为什么我总是在发布后知道答案,但这里是你如何知道用户输入的内容:

in your Adapter Class, in onBindViewHolder method, add textlistener for the EditText, here's an example :在您的适配器类中,在 onBindViewHolder 方法中,为 EditText 添加文本侦听器,这是一个示例:

holder.MyEditText.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) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            itemList.get(position).setText(s.toString());
        }
    });

Hope that helps you!希望能帮到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM