Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to pass my spinner list value into my other layout..

$
0
0

I retrieve data from my database using php and sync into my xamarin android app, retrieving and synchronizing is no problem, but my problem is how to pass that list of values that I put it into my spinner in my MainActivity into my other layout, my spinner is in my MainAcitvity layout but I don't want to place that spinner into my MainActivity, I want to place that into my other layout call ToexistingCustomer...Example when start my app the button sync and send are in my MainActivity and when I clicked the sync button and when synchronizing will become successful, all the data will stored and into an array and when I clicked the button send, the ToExistingCustomer layout will open and all my retrieve data that I stored into an array will displayed inside my ToExistingCustomer layout..

MainActivity.cs

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using MhylesApp.Synchronizer;

namespace MhylesApp
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        private Spinner spinner;
        string urlAddress = "http://_IPAddress_/php_config/config.php";
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
            spinner = FindViewById<Spinner>(Resource.Id.spinner);
            //spinner.SetBackgroundResource(Resource.Drawable.EditTxtStyle);
            Button sync = FindViewById<Button>(Resource.Id.sync);
            Button sendOrder = FindViewById<Button>(Resource.Id.sendOrder);
            sendOrder.Click += SendOrder_Click;    
            sync.Click += Sync_Click;
        }

        private void SendOrder_Click(object sender, System.EventArgs e)
        {
            StartActivity(typeof(ToExistingCustomer));
        }

        private void Sync_Click(object sender, System.EventArgs e)
        {
            new Syncher(this, urlAddress, spinner).Execute();
        }
    }
}

DataParser.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Org.Json;
using Exception = System.Exception;
using Object = Java.Lang.Object;
using String = System.String;

namespace MhylesApp.Synchronizer
{
    class DataParser : AsyncTask
    {
        Context c;
        private Spinner spinner;
        private String jsonData;
        JavaList<string> agent = new JavaList<string>();
#pragma warning disable CS0618 // Type or member is obsolete
        private ProgressDialog loadingProcess;
#pragma warning restore CS0618 // Type or member is obsolete
        public DataParser(Context c, Spinner spinner, string jsonData)
        {
            this.c = c;
            this.spinner = spinner;
            this.jsonData = jsonData;
        }
        protected override void OnPreExecute()
        {
            base.OnPreExecute();
#pragma warning disable CS0618 // Type or member is obsolete
            loadingProcess = new ProgressDialog(c);
#pragma warning restore CS0618 // Type or member is obsolete
            loadingProcess.SetTitle("Synchronizing");
            loadingProcess.SetMessage("Fetching Data...Please wait!");
            loadingProcess.Show();
        }
        protected override Object DoInBackground(params Object[] @params)
        {
            return this.ParseData();
        }
        protected override void OnPostExecute(Object result)
        {
            base.OnPostExecute(result);
            loadingProcess.Dismiss();
            if(Integer.ParseInt(result.ToString()) == 0)
            {
                Toast.MakeText(c, "Unable to parse data", ToastLength.Short).Show();
            }
            else
            {
                ToExistingCustomer toExistingCustomer = new ToExistingCustomer();
                ArrayAdapter<string> adapter = new ArrayAdapter<string>(c, Android.Resource.Layout.SimpleListItem1, agent);
                spinner.Adapter = adapter;

            }
        }
        private int ParseData()
        {
            try
            {
                JSONArray jsonArray = new JSONArray(jsonData);
                JSONObject jsonObj = null;

                agent.Clear();
                agent.Add("Select Recipient No.");
                for (int i = 0; i < jsonArray.Length(); i++)
                {
                    jsonObj = jsonArray.GetJSONObject(i);
                    String num = jsonObj.GetString("agent_contact");
                    agent.Add(num);
                }
                return 1;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            return 0;
        }
    }
}~~~~

Syncher.cs

using System;
using System.IO;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Widget;
using Java.IO;
using Java.Lang;
using Java.Net;
using Console = System.Console;
using Exception = System.Exception;
using Object = Java.Lang.Object;

namespace MhylesApp.Synchronizer
{
    class Syncher : AsyncTask
    {
        private Context c;
        private string urlAddress;
        private Spinner spinner;
#pragma warning disable CS0618 // Type or member is obsolete
        private ProgressDialog loadingProcess;
#pragma warning restore CS0618 // Type or member is obsolete
        public Syncher(Context c, string urlAddress, Spinner spinner)
        {
            this.c = c;
            this.urlAddress = urlAddress;
            this.spinner = spinner;
        }
        protected override void OnPreExecute()
        {
            base.OnPreExecute();
#pragma warning disable CS0618 // Type or member is obsolete
            loadingProcess = new ProgressDialog(c);
#pragma warning restore CS0618 // Type or member is obsolete
            loadingProcess.SetTitle("Synchronizing");
            loadingProcess.SetMessage("Fetching Data...Please wait!");
            loadingProcess.Show();
        }
        protected override Object DoInBackground(params Object[] @params)
        {
            return this.Retrieve();
        }
        protected override void OnPostExecute(Object result)
        {
            base.OnPostExecute(result);
            loadingProcess.Dismiss();
            if(result == null)
            {
                Toast.MakeText(c, "Synchronizing failed!", ToastLength.Short).Show();
            }
            else
            {
                DataParser parser = new DataParser(c, spinner, result.ToString());
                parser.Execute();
            }
        }
        //Retrieving Data
        private string Retrieve()
        {
            HttpURLConnection con = Connector.Connect(urlAddress);
            if(con == null)
            {
                return null;
            }
            try
            {
                Stream stream = new BufferedStream(con.InputStream);
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
                string line = null;
                StringBuffer response = new StringBuffer();
                while ((line = bufferedReader.ReadLine()) != null)
                {
                    response.Append(line + "\n");
                }
                bufferedReader.Close();
                stream.Close();
                return response.ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return null;
        }
    }
}``

Connector.cs

using System;
using Java.Net;

namespace MhylesApp.Synchronizer
{
    class Connector
    {
        public static HttpURLConnection Connect(String urlAddress)
        {
            try
            {
                URL url = new URL(urlAddress);
                HttpURLConnection con = (HttpURLConnection)url.OpenConnection();

                //Properties
                con.RequestMethod = "GET";
                con.ConnectTimeout = 20000;
                con.ReadTimeout = 20000;
                con.DoInput = true;
                return con;
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex);
            }
            return null;
        }
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>