Home > Software engineering >  How can I get the information in the Edittext contained in the Recyclerview?
How can I get the information in the Edittext contained in the Recyclerview?

Time:03-20

Data will be entered manually into the Edittext in the Recyclerview. How can I get the information entered in the edittext by clicking the button in the Activity? Can you show an example with code?

CodePudding user response:

Android RecyclerView with EditText example tutorial guide you to add edit text in every cell or child of the recycler view.

I have used a Model class to maintain proper data and the value of edit text in the recycler view.

You will also learn how to fetch the value of the EditText of all the rows. We will pass these values to the Next Activity.

The most common problems regarding this topic are

Value of edit text get change when scrolling Edittext loses content on scroll in recycler view

Edittext loses focus in recycler view Click More info

CodePudding user response:

If you want fast and easy solution, here is the one way for it.

Java version


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 version

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>
  • Related