2

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.

Community
  • 1
  • 1
Varun Agarwal
  • 1,587
  • 14
  • 29

1 Answers1

1

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.

Shane Sepac
  • 806
  • 10
  • 20
  • i didnt understand the code regarding public EditText et1 {}. Where exactly am I supposed to place it? And what does get; private set supposed to do? – Varun Agarwal Oct 09 '15 at 18:30
  • @VarunAgarwal It allows you to reference your EditTexts in the RecyclerView.Adapter subclass. You need these references so that you can get the string values from the EditTexts when your button is clicked. – Shane Sepac Oct 09 '15 at 18:33
  • I will try this once I get home. Will post if I face any issues. Thanks. Just one doubt, getEditTestString() is returning one set of edit texts. Wont I need to iterate through all the existing rows to collect the data? – Varun Agarwal Oct 09 '15 at 18:39
  • @VarunAgarwal Maybe you can explain better what you are trying to do. You are welcome to message me and I will try to help. But yes, you would need to iterate through all of them. It wasn't clear to me what you wanted. – Shane Sepac Oct 09 '15 at 18:42
  • My end goal is that my cashier will input name of an item that was bought, the quantity and the price for an order. Now with each order the number of items bought will keep changing so an option should be there to add more 'rows' to the recyclerview .So I need to create a form of sorts where they can input data and then I can extract all the data and save it, when a button outside the recyclerview is clicked. – Varun Agarwal Oct 09 '15 at 18:48