Forum Xamarin Xamarin.Android

how to add dynamic field values into a list....

JunCainJunCain Member ✭✭✭
edited March 2019 in Xamarin.Android

Product name, product quantity and product unit are dynamic fields, when I click the button generate product fields my app will generate Product name, product quantity and product unit fields, and my problem is how to add or store into a list each values of that three dynamic fields?

using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Java.Lang;
using Java.Net;
using System.IO;
using Console = System.Console;
using Exception = System.Exception;
using Object = Java.Lang.Object;
using MhylesApp.Synchronizer;
using Java.IO;
using System.Collections.Generic;
using Android.Content.PM;
using System;
using Android.Views;
using static Android.Provider.CalendarContract;
using Android.Text;

namespace MhylesApp
{
    [Activity(Label = "ToExistingCustomer", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Landscape)]
    public class ToExistingCustomer : Android.Support.V7.App.AppCompatActivity
    {
        //IList<string> agentData = new List<string>();
        IList<string> agentList = new List<string>();
        IList<string> custList = new List<string>();
        IList<string> prodList = new List<string>();
        IList<string> _prodList = new List<string>();
        TableLayout tbleLayout;
        TableRow tr, _tr;
        private static Spinner spinner, spinner1, spinner2, spinner3, _spinner;
        private EditText qty, unit, _td1, _td2;
        private TextView txtdate;
        //private TextView _spinner2;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ExistingCustomer);

            qty = FindViewById<EditText>(Resource.Id.qty);
            unit = FindViewById<EditText>(Resource.Id.unit);
            txtdate = FindViewById<TextView>(Resource.Id.txtdate);
            DateTime now = DateTime.Now.ToLocalTime();
            txtdate.Text = now.ToString();
#pragma warning disable CS0618 // Type or member is obsolete
            //unit.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
            //qty.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
#pragma warning restore CS0618 // Type or member is obsolete

            tbleLayout = FindViewById<TableLayout>(Resource.Id.tbleLayout);
            tr = FindViewById<TableRow>(Resource.Id.tr);
            //tbleLayout.SetColumnStretchable(0, true);
            //tbleLayout.SetColumnStretchable(1, true);

            //qty.SetBackgroundResource(Resource.Drawable.EditDesign);
            //unit.SetBackgroundResource(Resource.Drawable.EditDesign);




            agentList = Intent.GetStringArrayListExtra("agentList");
            agentList.Add("Select Agent Name");
            spinner = FindViewById<Spinner>(Resource.Id.spinner);

            custList = Intent.GetStringArrayListExtra("custList");
            custList.Add("Select Customer Name");
            spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);

            prodList = Intent.GetStringArrayListExtra("prodList");
            //prodList.Add("Select Product Name");
            spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);



            //spinner2.SetPromptId(Resource.String.product);
            //spinner2.SetBackgroundResource(Resource.Drawable.EditDesign);
            spinner3 = FindViewById<Spinner>(Resource.Id.spinner3);
            var btn = FindViewById<Button>(Resource.Id.btn);
            var adapter3 = ArrayAdapter.CreateFromResource(this, Resource.Array.Recipient, Android.Resource.Layout.SimpleSpinnerItem);
            adapter3.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
            spinner3.Adapter = adapter3;
            //spinner3.SetBackgroundResource(Resource.Drawable.EditTxtStyle);

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, agentList);
            spinner.Adapter = adapter;
            ArrayAdapter<string> adapter1 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, custList);
            spinner1.Adapter = adapter1;

            ArrayAdapter<string> adapter2 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, prodList);


            //adapter2.SetDropDownViewResource(Resource.Layout.productTextView);
            //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_margin));
            //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
            //spinner2.SetBackgroundResource(Resource.Drawable.DDArrow);
            //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
            //spinner2.SetPopupBackgroundResource(Resource.Drawable.spinnerPopBackground);
            //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
            prodList.Add("Select Product Name");
            spinner2.Adapter = adapter2;
            //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));

            //_prodList.Add(spinner2.ToString());
            btn.Click += (sender, e) =>
            {
                _tr = new TableRow(this);
                _spinner = new Spinner(this);
                _td1 = new EditText(this);
                _td2 = new EditText(this);
                if (tbleLayout.ChildCount == 12)
                {
                    Toast.MakeText(this, "Sorry, you can only order 10 products at a time", ToastLength.Short).Show();
                    return;
                }
                else
                {
                    //_td1.LayoutParameters = new LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);

                    _tr = new TableRow(new ContextThemeWrapper(this, Resource.Style.tableRow));


                    //_tr.SetPadding(2, 2, 2, 2);
                    //_tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams 0dp, ))


                    //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //TextInputLayout textInputLayout = new TextInputLayout(this);
                    //TextInputLayout textInputLayout1 = new TextInputLayout(this);
                    _td1.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    //_td1.Visibility = ViewStates.Visible;
                    //_td1.SetPadding(15, 15, 15, 15);
                    _td1.SetBackgroundResource(Resource.Drawable.auto_borderColor);
#pragma warning disable CS0618 // Type or member is obsolete
                    _td1.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
#pragma warning restore CS0618 // Type or member is obsolete
                    _td1.SetHint(Resource.String.qty);

                    _td2.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
                    //_td2.Visibility = ViewStates.Visible;
                    //_td2.SetPadding(15, 15, 15, 15);
                    _td2.SetBackgroundResource(Resource.Drawable.auto_borderColor);
#pragma warning disable CS0618 // Type or member is obsolete
                    _td2.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
#pragma warning restore CS0618 // Type or member is obsolete
                    _td2.SetHint(Resource.String.unit);

                    //textInputLayout.AddView(_td1);
                    //textInputLayout11.AddView(_td2);
                    //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
                    //_spinner.SetBackgroundResource(Resource.Drawable.EditTxtStyle);

                    //_tr.AddView(textInputLayout);
                    //_spinner = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
                    _spinner.SetBackgroundResource(Resource.Drawable.auto_DDarrow);

                    _spinner.SetPopupBackgroundResource(Resource.Drawable.spinnerPopBackground);

                    //_spinner.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
                    ArrayAdapter<string> _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleDropDownItem1Line, prodList);
                    //_adapter.SetDropDownViewResource(Resource.Layout.productTextView);
                    _spinner.Adapter = _adapter;

                    _tr.AddView(_spinner);

                    //_td1.LayoutParameters = _params;
                    //ViewGroup.LayoutParams _params = _td1.getLayoutParams();

                    //_td1 = new EditText(new ContextThemeWrapper(this, Resource.Style.edit_margin));

                    _tr.AddView(_td1);
                    _tr.AddView(_td2);
                    tbleLayout.AddView(_tr);
                    //_prodList.Add(_spinner);
                    if(!TextUtils.IsEmpty(_td1.Text.ToString()) && !TextUtils.IsEmpty(_td2.Text.ToString()) && !TextUtils.IsEmpty(_spinner.ToString()))
                    {
                        return;
                    }
                    else
                    {
                        _prodList.Add(_spinner.ToString() + "|" + _td1.Text.ToString() + "|" + _td2.Text.ToString());
                    }


                }



            };


        }

    }

}

Answers

  • JunCainJunCain Member ✭✭✭

    here is the updated UI

  • jezhjezh Member, Xamarin Team Xamurai

    Have you resolved this problem?

  • JunCainJunCain Member ✭✭✭
    edited March 2019

    no, but trying to figure it out right now..

  • jezhjezh Member, Xamarin Team Xamurai

    If the number of newly generated controls is not large, you can name each control individually and use it.
    e.g. _spinner1 _td1_1, _td2_1,
    _spinner2 _td1_2, _td2_2
    ......

    By the way , I couldn't fully get the meaning of your code.
    Could you please post a basic demo ?

  • JunCainJunCain Member ✭✭✭

    I want to put each of table rows values, like spinner values, edittext values into a list...but the problem is, I don't know how to do it..My spinner and edittext are auto generated fields that's why it so hard for me to figure it out.

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Widget;
    using Java.Lang;
    using Java.Net;
    using System.IO;
    using Console = System.Console;
    using Exception = System.Exception;
    using Object = Java.Lang.Object;
    using MhylesApp.Synchronizer;
    using Java.IO;
    using System.Collections.Generic;
    using Android.Content.PM;
    using System;
    using Android.Views;
    using MhylesApp.Database;
    using Android.Database;
    using String = System.String;
    using MhylesApp.Model;
    using System.Linq;
    using MhylesApp.CustomAdapter;
    using MhylesOrderingApp;
    
    namespace MhylesApp
    {
        [Activity(Label = "ToExistingCustomer", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation, ScreenOrientation = ScreenOrientation.Landscape)]
        public class ToExistingCustomer : Android.Support.V7.App.AppCompatActivity
        {
            TableLayout tbleLayout;
            TableRow tr, _tr;
            private static Spinner spinner, spinner1, spinner2, spinner3, _spinner;
            private EditText qty, unit, _td1, _td2;
            private TextView txtdate;
            private static int agentID, custID, prodID;
            private static Button removeBtn;
            private static String agentName, custName, prodName, _prodId, __prodId;
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.ExistingCustomer);
    
                qty = FindViewById<EditText>(Resource.Id.qty);
                unit = FindViewById<EditText>(Resource.Id.unit);
                txtdate = FindViewById<TextView>(Resource.Id.txtdate);
                DateTime now = DateTime.Now.ToLocalTime();
                txtdate.Text = now.ToString();
    
    
                tbleLayout = FindViewById<TableLayout>(Resource.Id.tbleLayout);
                tr = FindViewById<TableRow>(Resource.Id.tr);
    
                removeBtn = FindViewById<Button>(Resource.Id.btnRemove);
    
                var btnSend = FindViewById<Button>(Resource.Id.btnSend);
    
    
                spinner = FindViewById<Spinner>(Resource.Id.spinner);
    
                spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);
    
                spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);
    
                //spinner2 = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
    
                spinner3 = FindViewById<Spinner>(Resource.Id.spinner3);
                var btn = FindViewById<Button>(Resource.Id.btn);
                var adapter3 = ArrayAdapter.CreateFromResource(this, Resource.Array.Recipient, Android.Resource.Layout.SimpleSpinnerItem);
                adapter3.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
                spinner3.Adapter = adapter3;
    
                removeBtn.Click += RemoveBtn_Click;
    
                spinner2.ItemSelected += Spinner2_ItemSelected;
    
    
                GetProdData();
                GetAgentData();
                GetCustData();
                //<Generate new table row, spinnerand edittext>//
                btn.Click += (sender, e) =>
                {
    
                    _tr = new TableRow(this);
                    _spinner = new Spinner(this);
                    _td1 = new EditText(this);
                    _td2 = new EditText(this);
                    if (tbleLayout.ChildCount == 12)
                    {
                        Toast.MakeText(this, "Sorry, you can only order 10 products at a time", ToastLength.Short).Show();
                        return;
                    }
                    else
                    {
    
                        _tr = new TableRow(new ContextThemeWrapper(this, Resource.Style.tableRow));
    
                        _td1.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
    
                        _td1.SetBackgroundResource(Resource.Drawable.auto_borderColor);
    #pragma warning disable CS0618 // Type or member is obsolete
                        _td1.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
    #pragma warning restore CS0618 // Type or member is obsolete
                        _td1.SetHint(Resource.String.qty);
    
                        _td2.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
    
                        _td2.SetBackgroundResource(Resource.Drawable.auto_borderColor);
    #pragma warning disable CS0618 // Type or member is obsolete
                        _td2.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
    #pragma warning restore CS0618 // Type or member is obsolete
                        _td2.SetHint(Resource.String.unit);
    
    
                        _spinner = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
                        _spinner.SetBackgroundResource(Resource.Drawable.auto_DDarrow);
    
                        _spinner.SetPopupBackgroundResource(Resource.Drawable.spinnerPopBackground);
    
                        _spinner.Adapter = new ProductAdapter(prodData);
    
                        _tr.AddView(_spinner);
    
                        _tr.AddView(_td1);
                        _tr.AddView(_td2);
                        tbleLayout.AddView(_tr);
    
                        _spinner.ItemSelected += _spinner_ItemSelected;
    
                    }
    
                };
    
            }
            //<.......................>///
            //<Remove only the generated table row>//
            private void RemoveBtn_Click(object sender, EventArgs e)
            {
                int childCount = tbleLayout.ChildCount;
                if(childCount > 3)
                {
                    tbleLayout.RemoveViewAt(childCount - 1);
                }
                else
                {
                    Toast.MakeText(this, "Sorry, you can't delete the last product fields!", ToastLength.Short).Show();
                }
            }
            //<......................>//
            //<This block of code is not final>//
            private void _spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                __prodId = prodData[e.Position].prodId.ToString();
            }
            private void Spinner2_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                _prodId = prodData[e.Position].prodId.ToString();
            }
            //<..................................>//
            //<Fetching from sqlite>//
            private void GetProdData()
            {
                prodData.Clear();
                DBAdapter db = new DBAdapter(this);
                db.openDB();
                ICursor _cur = db.GetProdData();
                while (_cur.MoveToNext())
                {
                    prodID = _cur.GetInt(0);
                    prodName = _cur.GetString(1);
                    ProdDataBinding();
                }
                db.closeDB();
            }
            //<...............>//
            //<Data binding>//
            private static List<ProdModel> prodData = new List<ProdModel>();
            private static void ProdDataBinding()
            {
                ProdModel _prodModel = new ProdModel()
                {
                    prodId = prodID.ToString(),
                    prodName = prodName.ToString()
                };
                prodData.OrderBy(i => i.prodId).ToString();
                prodData.Add(_prodModel);
                if (prodData.Count > 0)
                {
                    spinner2.Adapter = new ProductAdapter(prodData);
    
                }
            }
            //<....................>//
            //<Fetching from sqlite>//
            private void GetCustData()
            {
                custData.Clear();
                DBAdapter db = new DBAdapter(this);
                db.openDB();
                ICursor _cur = db.GetCustData();
                while (_cur.MoveToNext())
                {
                    custID = _cur.GetInt(0);
                    custName = _cur.GetString(1);
                    CustDataBinding();
                }
    
                db.closeDB();
            }
            //<....................>//
            //<Data binding>//
            private static List<CustModel> custData = new List<CustModel>();
            private static void CustDataBinding()
            {
                CustModel _custModel = new CustModel()
                {
                    custID = custID.ToString(),
                    custName = custName.ToString()
                };
                custData.OrderBy(i => i.custID).ToString();
                custData.Add(_custModel);
                if (custData.Count > 0)
                {
                    spinner1.Adapter = new CustomerAdapter(custData);
                }
            }
            //<....................>//
            //<Fetching from sqlite>//
            private void GetAgentData()
            {
                agentData.Clear();
                DBAdapter db = new DBAdapter(this);
                db.openDB();
                ICursor _cur = db.GetAgentData();
                while (_cur.MoveToNext())
                {
                    agentID = _cur.GetInt(0);
                    agentName = _cur.GetString(1);
                    AgentDataBinding();
                }
    
                db.closeDB();
            }
            //<....................>//
            //<Data binding>//
            private static List<AgentModel> agentData = new List<AgentModel>();
            private static void AgentDataBinding()
            {
                AgentModel _agentModel = new AgentModel()
                {
                    agentID = agentID.ToString(),
                    agentName = agentName.ToString()
    
                };
                agentData.OrderBy(i => i.agentID).ToString();
                agentData.Add(_agentModel);
                if(agentData.Count > 0)
                {
                    spinner.Adapter = new AgentAdapter(agentData);
                }
            }
            //<....................>//
        }
    
    }
    
  • JunCainJunCain Member ✭✭✭
    edited March 2019

    generate products fields button can generate up to 10 rows.

  • JunCainJunCain Member ✭✭✭

    Now I got it. Problem solved :)

    Code updated

     btn.Click += (sender, e) =>
                {
    
                    _tr = new TableRow(this);
                    _spinner = new Spinner(this);
                    _td1 = new EditText(this);
                    _td2 = new EditText(this);
    
                    if (tbleLayout.ChildCount == 12)
                    {
                        Toast.MakeText(this, "Sorry, you can only order 10 products at a time", ToastLength.Short).Show();
                        return;
                    }
                    else
                    {
    
                        _tr = new TableRow(new ContextThemeWrapper(this, Resource.Style.tableRow));
    
                        _td1.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
    
                        _td1.SetBackgroundResource(Resource.Drawable.auto_borderColor);
    #pragma warning disable CS0618 // Type or member is obsolete
                        _td1.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
    #pragma warning restore CS0618 // Type or member is obsolete
                        _td1.SetHint(Resource.String.qty);
    
                        _td2.LayoutParameters = new TableRow.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);
    
                        _td2.SetBackgroundResource(Resource.Drawable.auto_borderColor);
    #pragma warning disable CS0618 // Type or member is obsolete
                        _td2.SetTextColor(Resources.GetColorStateList(Resource.Drawable.myColors));
    #pragma warning restore CS0618 // Type or member is obsolete
                        _td2.SetHint(Resource.String.unit);
    
    
                        _spinner = new Spinner(new ContextThemeWrapper(this, Resource.Style.edit_spinner));
                        _spinner.SetBackgroundResource(Resource.Drawable.auto_DDarrow);
    
                        _spinner.SetPopupBackgroundResource(Resource.Drawable.spinnerPopBackground);
    
                        _spinner.Adapter = new ProductAdapter(prodData);
    
                        _tr.AddView(_spinner);
    
                        _tr.AddView(_td1);
                        _tr.AddView(_td2);
                        tbleLayout.AddView(_tr);
    
    
                        _td1.TextChanged += _td1_TextChanged;
                        _td2.TextChanged += _td2_TextChanged;
                        _spinner.ItemSelected += _spinner_ItemSelected;
                        for (int i = 3; i < tbleLayout.ChildCount; i++)
                        {
                            String _var = __prodId;
                            String var1 = __prodQty;
                            String var2 = __prodUnit;
    
                            prodArray.Add(_var + "|" + var1 + "|" + var2);
                        }
    
                    }
    
                };
    
            }
    
            private void _td2_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
            {
                __prodUnit = e.Text.ToString();
            }
    
            private void _td1_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
            {
                __prodQty = e.Text.ToString();
            }
    
            private void _spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
            {
                __prodId = prodData[e.Position].prodId.ToString();
            }
    
Sign In or Register to comment.