Varun Agarwal
Varun Agarwal

Reputation: 1587

Retrieve multiple edit text per row from RecyclerView

I have read through this link and this one but they speak about getting one edit text per row from a recyclerview but in my case I want to add 3 edit text per row in recyclerview and then extract all the strings from these edit text when a button outside the recyclerview is clicked.

The layout for the recyclerview will be something like this. Each will be an edit text in which the user will input name, number and price of an item.

________________________________________________________-
|__Name__________|___Number________|____Price___________|+|
________________________________________________________-
|__Name__________|___Number________|____Price___________|+|
________________________________________________________-
|__Name__________|___Number________|____Price___________|+|

Could someone please help me out this problem.

Upvotes: 2

Views: 1829

Answers (1)

Shane Sepac
Shane Sepac

Reputation: 826

In your RecyclerView.Adapter subclass, let's call it customRAdapter, inflate a layout in your overridden OnCreateViewHolder method that contains three EditTexts in a vertically oriented LinearLayout. Next, add a public method to your customRAdapter class that returns an array of strings containing the text in your three EditTexts. When your button is clicked from outside of the RecyclerView, reference the customRAdapter instance's method that returns the array of strings and your problem is solved.

Your XML layout to be inflated in the overridden OnCreateViewHolder method (LAYOUT_NAME.axml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/holo_blue_dark"
android:id="@+id/main_framelayout">

<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText1"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText2"
/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/EditText3"
/>
</LinearLayout>

The overridden OnCreateViewHolder method itself:

    public override RecyclerView.ViewHolder OnCreateViewHolder (ViewGroup parent, int viewType)
    {
    layoutinflater = LayoutInflater.From (context);
    view = layoutinflater.Inflate (Resource.Layout.LAYOUT_NAME, parent, false);
    return new customRecyclerHolder (view);
    }

In your RecyclerView.ViewHolder subclass have the following:

        public EditText et1 {
            get;
            private set; 
        }
        public EditText et2 {
            get;
            private set; 
        }
        public EditText et3 {
            get;
            private set; 
        }
        public customRecyclerHolder(View view):base(view){

        et1 = view.FindViewById<EditText>(Resource.Id.EditText1);
        et2 = view.FindViewById<EditText>(Resource.Id.EditText2);
        et3 = view.FindViewById<EditText>(Resource.Id.EditText3);
        }

An example of getting the array of strings (inside your customRAdapter class):

customViewHolder holder;
public override void OnBindViewHolder (RecyclerView.ViewHolder holder, int position)
    {
                ....
    this.holder = holder as customViewHolder; //get the reference to the current viewholder
    }

    //call this method from your button code to retrieve the strings. 
    public string[] getEditTextStrings(){
        return new string[]{ holder.et1.Text, holder.et2.Text, holder.et3.Text };
    }

FYI, this is all in C#. The principles and almost all of the code is about the same in Java.

Upvotes: 1

Related Questions