Get data from list of EditTexts in a recycler view to a list?

523 Views Asked by At
I am having an EditText in a TTextInputLayout as a Row_item for my recycler view, On the click of a button I am incrementing the count value in that recycler view and adding the edit text views in the app. After having as many views as I want, I am putting some data in those EditTexts and After all this I press save button and all the data from EditTexts should save in a list. I tried the textwatcher Property in xamarin.android but not able to perform that.

In my activity I also tried

var view = addChildRecyclerView.FindViewHolderForAdapterPosition(i);
                var holder = view as fragmentRecyclerViewAdapterViewHolder;
                if (holder != null)
                {
                    holder.childname.FindViewById<TextInputLayout>(Resource.Id.enterChildNameTextView);
                   
                    childlisttosend.Add(holder.childname.EditText.Text);

But sometimes I get holder as a null and some valus are missed to be put in the list. I tried textchange property but it puts every single alphabet in the list, like

for index 1 it is "m"
for index 2 it is "ma"
for index 3 it is "man"
for index 4 it is "mani"
for index 5 it is "manis"
for index 6 it is "manish"

It is of no use. Please sugggest a solution. adapter is

namespace assessment1_part2_v2.Fragment
{
    public class addFamilyFragment : DialogFragment
    {
        public TextInputLayout fathername, mothername, address;
        public MaterialButton addchild, savebutton;
        public ArrayAdapter adapter;
        public ListView listview;
        public familyData familydata = new familyData();
        public List<childData> childrenList = new List<childData>();
        public editTextAdapter EditTextAdapter;
        public event EventHandler<DataSenderClass> famaDataSender;
        public RecyclerView enterChildRecyclerView;
        public List<string> addChildDummyData = new List<string>();
        public TextInputLayout enterChildEditTExt;

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.addfam_layout, container, false);
            connectingViews(view);
            buttonClicks();
            return view;
        }

        public class DataSenderClass : EventArgs
        {
            public familyData data { get; set; }
        }

        private void buttonClicks()
        {
            addchild.Click += Addchild_Click;
            savebutton.Click += Savebutton_Click;
        }

        private void Savebutton_Click(object sender, EventArgs e)
        {


            //father, mother and address
            familydata.fathername = fathername.EditText.Text;
            familydata.mothername = mothername.EditText.Text;
            familydata.location = address.EditText.Text;
            familydata.children = new List<childData>();
            List<childData> list = new List<childData>();
            List<string> dummyCheck = new List<string>();
             for (int i = 0; i <= addChildDummyData.Count; i++)
            {
                var viewholder = enterChildRecyclerView.FindViewHolderForAdapterPosition(i);
                
                var holder = viewholder as editTextAdapterViewHolder;
               // holder.addChildEditTExt;
                if(holder != null)
                {
                    holder.addChildEditTExt.FindViewById<TextInputLayout>(Resource.Id.enterChildEditTExt);
                    dummyCheck.Add(holder.addChildEditTExt.EditText.Text);
                }

            }
            var x = dummyCheck;
            addChildDummyData.Clear();
            famaDataSender?.Invoke(this, new DataSenderClass { data = familydata });
            this.Dismiss();
        }

        private void Addchild_Click(object sender, EventArgs e)
        {
           
            if (addChildDummyData.Count == 0)
            {
                addChildDummyData.Add("");
                EditTextAdapter = new editTextAdapter(addChildDummyData);
                enterChildRecyclerView.SetLayoutManager(new LinearLayoutManager(Activity));
                enterChildRecyclerView.SetAdapter(EditTextAdapter);
                int x = enterChildRecyclerView.ChildCount;
            }
            else
            {
                addChildDummyData.Add("");
                EditTextAdapter.NotifyItemInserted(addChildDummyData.Count - 1);
                int x = enterChildRecyclerView.ChildCount;
            }
        }

        private void connectingViews(View view)
        {
            fathername = view.FindViewById<TextInputLayout>(Resource.Id.enterFatherNameEditText);
            mothername = view.FindViewById<TextInputLayout>(Resource.Id.enterMotherNameEditText);
            address = view.FindViewById<TextInputLayout>(Resource.Id.enterAddressEditText);
            addchild = view.FindViewById<MaterialButton>(Resource.Id.addChildButton);
            savebutton = view.FindViewById<MaterialButton>(Resource.Id.saveButton);
            enterChildRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.enterChildRecyclerView);
            //listview = (ListView)view.FindViewById(Resource.Id.enterChildRecyclerView);
        }
    }
}                   `
1

There are 1 best solutions below

0
Jessie Zhang -MSFT On

We couldn't see the code of editTextAdapter.

But you can refer to my code ,my RecyclerView has an Edittext in each item and we can get data from the input list.

I put TextWatcher in my RecyclerView.ViewHolder(PhotoViewHolder.cs).

PhotoViewHolder.cs

public class PhotoViewHolder: RecyclerView.ViewHolder
{
    public ImageView Image { get; private set; }
    public TextView Caption { get; private set; }
    public CheckBox MyCheckBox { get; set; }
    public bool IsChecked { get; set; }

    public EditText MyEditText { get; set; }

     public MyTextWatcher myTextWatcher;


    public Button imAddBefore { get; private set; }
    public Button imAddAfter { get; private set; }

    public PhotoViewHolder(View itemView, Action<int> listener, MyTextWatcher myWatcher  ) : base(itemView)
    {
        //Image = itemView.FindViewById<ImageView> (Resource.Id.imageView);
        Caption = itemView.FindViewById<TextView>(Resource.Id.textView);
        MyCheckBox = itemView.FindViewById<CheckBox>(Resource.Id.myCheckBox);

        DeleteButton = itemView.FindViewById<Button>(Resource.Id.deleteBtn);

        MyEditText = itemView.FindViewById<EditText>(Resource.Id.mEditText);
        this.myTextWatcher = myWatcher;
         // add TextWatcher here
        MyEditText.AddTextChangedListener(myTextWatcher);


       // other code

    }


    public class MyTextWatcher : Java.Lang.Object, ITextWatcher
    {
        int position;

        public void AfterTextChanged(IEditable s)
        {
        }

        public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
        {
        }

        public void OnTextChanged(ICharSequence s, int start, int before, int count)
        { //mInput  is a field in my item model
            PhotoAlbumAdapter.mPhotoAlbum[position].mInput = s.ToString();
        }

        public void updatePosition(int position)
        {
            this.position = position;
        }

    }
}

My RecyclerView.Adapter is PhotoAlbumAdapter.cs

PhotoAlbumAdapter.cs

  public class PhotoAlbumAdapter: RecyclerView.Adapter
  {
    public event EventHandler<int> ItemClick;

    public static List<Photo> mPhotoAlbum = new List<Photo>();

    public static RecyclerView.Adapter adapter;

    public PhotoAlbumAdapter(List<Photo> branchesList)
    {
        adapter = this;
        mPhotoAlbum = branchesList;
    }


    public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
    {
        View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.PhotoCardView, parent, false);

        PhotoViewHolder vh = new PhotoViewHolder(itemView, OnClick, new PhotoViewHolder.MyTextWatcher());
       
        return vh;
    }

    public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        PhotoViewHolder vh = holder as PhotoViewHolder;

        Photo item=   mPhotoAlbum[position];

        vh.Caption.Text = item.Caption;
        
        // here we call function `updatePosition` in our TextWatcher class
        vh.myTextWatcher.updatePosition(vh.AdapterPosition);

        vh.MyEditText.Text = item.mInput;

        // other code
    }


    public override int ItemCount
    {
        get { return mPhotoAlbum.Count; }
    }

    // Raise an event when the item-click takes place:
    void OnClick(int position)
    {
        if (ItemClick != null)
            ItemClick(this, position);
    }
}

In my activity, we can get the data from the recycleview like this:

  List<Photo> items= new List<Photo>();

  mAdapter = new PhotoAlbumAdapter(items);
  mRecyclerView.SetAdapter(mAdapter);

The result is:

  for (int i=0;i< items.Count;i++) {
    Photo temp = items[i];
    System.Diagnostics.Debug.WriteLine( "The "+i +" result is: "+" Caption= " + temp.Caption + "<---> input = " + temp.mInput);

   }