Advertisement
HyugaPaolo

APP #1

Apr 25th, 2018
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. //ACTIVITY 1
  2. sing Android.App;
  3. using Android.Content;
  4. using Android.OS;
  5. using Android.Widget;
  6. using Java.Net;
  7. using System;
  8. using System.IO;
  9. using System.Json;
  10. using System.Net;
  11. using System.Security.Policy;
  12. using System.Threading.Tasks;
  13.  
  14. namespace Prova
  15. {
  16.     [Activity(Label = "Prova", MainLauncher = true)]
  17.     public class MainActivity : Activity
  18.     {
  19.         JsonValue MyResult;
  20.         JsonValue[] Indice;
  21.        
  22.         protected override void OnCreate(Bundle savedInstanceState)
  23.         {
  24.             base.OnCreate(savedInstanceState);
  25.  
  26.             // Set our view from the "main" layout resource
  27.             SetContentView(Resource.Layout.PorcaMadonna);
  28.             Button MyButton = FindViewById<Button>(Resource.Id.button1);
  29.             EditText MyText = FindViewById<EditText>(Resource.Id.textView1);
  30.             ListView MyList = FindViewById<ListView>(Resource.Id.listView1);
  31.  
  32.             MyButton.Click += async (sender, e) =>
  33.             {
  34.                 string url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurant+in+" + MyText.Text + "&key=METTERELAKEY";
  35.                 JsonValue json = await FetchMethodAsync(url);
  36.                 MyResult = json["results"];
  37.                 string[] items = new string[MyResult.Count];
  38.                 int i = 0;
  39.                 Indice = new JsonValue[MyResult.Count];
  40.                 foreach (JsonValue x in MyResult)
  41.                 {
  42.  
  43.                     items[i] = x["name"];
  44.                     Indice[i] = x;
  45.                     i++;
  46.                 }
  47.                 MyList.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
  48.  
  49.                 MyList.ItemClick += MyList_ItemClick;
  50.  
  51.             };
  52.  
  53.  
  54.         }
  55.  
  56.         private void MyList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
  57.         {
  58.             Intent MyIntent = new Intent(this, typeof(activity2));
  59.             MyIntent.PutExtra("Ciao", Indice[e.Position].ToString());
  60.             StartActivity(MyIntent);
  61.         }
  62.  
  63.         private async Task<JsonValue> FetchMethodAsync(string url)
  64.         {
  65.             string c = url;
  66.             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
  67.             request.ContentType = "application/json";
  68.             request.Method = "GET";
  69.  
  70.             using (WebResponse response = await request.GetResponseAsync())
  71.             {
  72.                 using (Stream stream = response.GetResponseStream())
  73.                 {
  74.                     JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
  75.  
  76.                     return jsonDoc;
  77.                 }
  78.             }
  79.         }
  80.  
  81.     }
  82. }
  83. //ACTIVITY 2
  84.  
  85.  
  86. using System;
  87. using System.Collections.Generic;
  88. using System.Linq;
  89. using System.Text;
  90. using System.Json;
  91. using Android.App;
  92. using Android.Content;
  93. using Android.OS;
  94. using Android.Runtime;
  95. using Android.Views;
  96. using Android.Widget;
  97.  
  98. namespace Prova
  99. {
  100.     [Activity(Label = "activity2")]
  101.     public class activity2 : Activity
  102.     {
  103.         protected override void OnCreate(Bundle savedInstanceState)
  104.         {
  105.             base.OnCreate(savedInstanceState);
  106.             SetContentView(Resource.Layout.layout1);
  107.             TextView MyText1 = FindViewById<TextView>(Resource.Id.textView1);
  108.             TextView MyText2 = FindViewById<TextView>(Resource.Id.textView2);
  109.             JsonValue A = (JsonValue)Intent.GetStringExtra("Ciao");
  110.             MyText1.Text = A["name"];
  111.             MyText2.Text = A["formatted_address"];
  112.  
  113.  
  114.             // Create your application here
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement