Advertisement
Guest User

Android App

a guest
Apr 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. using Android.App;
  2. using Android.Widget;
  3. using Android.OS;
  4.  
  5. using SQLite;
  6. using Android;
  7. using System;
  8. using System.Collections;
  9.  
  10. namespace whatever
  11. {
  12. [Activity(Label = "whatever", MainLauncher = true)]
  13. public class MainActivity : Activity
  14. {
  15. TextView textView;
  16. EditText ime;
  17. EditText cena;
  18. protected override void OnCreate(Bundle savedInstanceState)
  19. {
  20. base.OnCreate(savedInstanceState);
  21.  
  22. // Set our view from the "main" layout resource
  23. SetContentView(Resource.Layout.Main);
  24.  
  25. Button button_create_db = FindViewById<Button>(Resource.Id.button1);
  26. button_create_db.Click += Button_create_db_Click1;
  27. textView = FindViewById<TextView>(Resource.Id.textView1);
  28.  
  29. Button button_fill_spinner = FindViewById<Button>(Resource.Id.button2);
  30. button_fill_spinner.Click += Button_fill_spinner_Click;
  31.  
  32. Button button_delete = FindViewById<Button>(Resource.Id.button3);
  33. button_delete.Click += Button_delete_Click;
  34. }
  35.  
  36. private void Button_delete_Click(object sender, EventArgs e)
  37. {
  38. string dbPath = System.IO.Path.Combine(
  39. System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
  40. "sales.db3");
  41. var db = new SQLiteConnection(dbPath);
  42.  
  43. db.DropTable<Products>();
  44. //throw new NotImplementedException();
  45. }
  46.  
  47. private void Button_fill_spinner_Click(object sender, EventArgs e)
  48. {
  49. Spinner spinner = FindViewById<Spinner>(Resource.Id.spinner1);
  50.  
  51. string dbPath = System.IO.Path.Combine(
  52. System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
  53. "sales.db3");
  54.  
  55. var sales = new SQLiteConnection(dbPath);
  56.  
  57. ArrayList result = new ArrayList();
  58.  
  59. var table = sales.Table<Products>();
  60.  
  61. foreach (var s in table)
  62. {
  63. result.Add(s.Name);
  64. }
  65.  
  66. string[] result_string = (string[])result.ToArray(typeof(string));
  67.  
  68. var spiner_adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem, result_string);
  69.  
  70. spiner_adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
  71.  
  72. spinner.Adapter = spiner_adapter;
  73. }
  74.  
  75.  
  76.  
  77. private void Button_create_db_Click1(object sender, EventArgs e)
  78. {
  79. string dbPath = System.IO.Path.Combine(
  80. System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
  81. "sales.db3");
  82.  
  83. var sales = new SQLiteConnection(dbPath);
  84.  
  85. sales.CreateTable<Products>();
  86.  
  87. var newProduct = new Products();
  88.  
  89. newProduct.Name = "Keyboard";
  90. newProduct.Price = 10;
  91. sales.Insert(newProduct);
  92.  
  93. newProduct.Name = "Mouse";
  94. newProduct.Price = 15;
  95. sales.Insert(newProduct);
  96.  
  97. newProduct.Name = "Printer";
  98. newProduct.Price = 20;
  99. sales.Insert(newProduct);
  100.  
  101. ime = FindViewById<EditText>(Resource.Id.editText1);
  102. cena = FindViewById<EditText>(Resource.Id.editText2);
  103.  
  104. newProduct.Name = ime.Text;
  105. newProduct.Price = float.Parse(cena.Text);
  106. sales.Insert(newProduct);
  107.  
  108.  
  109. }
  110. [Table("products")]
  111. public class Products
  112. {
  113. [PrimaryKey, AutoIncrement, Column("_id")]
  114. public int Id { get; set; }
  115. [MaxLength(8)]
  116. public string Name { get; set; }
  117. public float Price { get; set; }
  118. }
  119.  
  120. }
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement