如何获取 Recyclerview 中包含的 Edittext 中的信息?

How can I get the information in the Edittext contained in the Recyclerview?

数据将手动输入到 Recyclerview 的 Edittext 中。如何通过单击 Activity 中的按钮获取编辑文本中输入的信息?你能用代码展示一个例子吗?

如果您想要快速简单的解决方案,这里有一种方法。

Java版本


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = findViewById(R.id.rec);
        Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);
        Button submit = findViewById(R.id.submit);
        submit.setOnClickListener(v -> {
            HashMap<Integer, String> data = adapter.getData();
            Log.d("___________", "Here is our data from recyclerview" + data.toString());
        });
    }
}

class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    HashMap<Integer, String> dataHolder = new HashMap();

    HashMap<Integer, String> getData() {
        return dataHolder;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.bind(holder.itemView, position);
    }

    @Override
    public int getItemCount() {
        return 3;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        void bind(View itemView, Integer position) {
            EditText input = itemView.findViewById(R.id.input);
            input.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) {
                    dataHolder.put(position, s.toString());
                }
            });
        }
    }
}

Kotlin 版本

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    private val recView by lazy { findViewById<RecyclerView>(R.id.rec) }
    private val submit by lazy { findViewById<Button>(R.id.submit) }
    private val adapter by lazy { Adapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recView.adapter = adapter
        submit.setOnClickListener {
            val data = adapter.getData()
            Log.e("_____________", "data $data")

        }
    }

    class Adapter() : RecyclerView.Adapter<Adapter.ViewHolder>() {
        private val dataHolder = hashMapOf<Int, String>()
        fun getData(): HashMap<Int, String> {
            return dataHolder
        }

        inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
            fun bind(itemView: View, position: Int) {
                val input = itemView.findViewById<EditText>(R.id.input)
                input.doAfterTextChanged {
                    dataHolder[position] = it.toString()
                }
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false))
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) =
            holder.bind(holder.itemView, position)

        override fun getItemCount(): Int = 3
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rec"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/item" />

    <Space
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>