Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. package com.a4ci.fastorder;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.net.wifi.WifiInfo;
  7. import android.net.wifi.WifiManager;
  8. import android.os.Bundle;
  9. import android.support.design.widget.FloatingActionButton;
  10. import android.support.design.widget.NavigationView;
  11. import android.support.v4.view.GravityCompat;
  12. import android.support.v4.widget.DrawerLayout;
  13. import android.support.v7.app.ActionBarDrawerToggle;
  14. import android.support.v7.app.AppCompatActivity;
  15. import android.support.v7.widget.Toolbar;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.view.View;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21.  
  22. import java.io.Serializable;
  23. import java.sql.Connection;
  24. import java.sql.DriverManager;
  25. import java.sql.ResultSet;
  26. import java.sql.SQLException;
  27. import java.sql.Statement;
  28. import java.util.ArrayList;
  29.  
  30. public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  31.  
  32.     //rivate ArrayList<Prodotto> carrello=new ArrayList<>();
  33.     private TextView reteTV;
  34.     private Connection con;
  35.     private Thread t1;
  36.     private ArrayList<Prodotto> prodotti;
  37.  
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.activity_main);
  42.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  43.         setSupportActionBar(toolbar);
  44.  
  45.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  46.         fab.setOnClickListener(new View.OnClickListener() {
  47.             @Override
  48.             public void onClick(View view) {
  49.                 startActivity(new Intent(getApplicationContext(), ChoseList.class));
  50.             }
  51.         });
  52.  
  53.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  54.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  55.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  56.         drawer.setDrawerListener(toggle);
  57.         toggle.syncState();
  58.  
  59.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  60.         navigationView.setNavigationItemSelectedListener(this);
  61.  
  62.  
  63.         //OGGETTI GRAFICI
  64.         reteTV = (TextView) findViewById(R.id.reteTV);
  65.  
  66.         //METODI
  67.         prodotti = new ArrayList<>();
  68.         setConnectionTV();
  69.         importData();
  70.  
  71.  
  72.     }
  73.  
  74.     @Override
  75.     public void onBackPressed() {
  76.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  77.         if (drawer.isDrawerOpen(GravityCompat.START)) {
  78.             drawer.closeDrawer(GravityCompat.START);
  79.         } else {
  80.             super.onBackPressed();
  81.         }
  82.     }
  83.  
  84.    /* @Override
  85.     public boolean onCreateOptionsMenu(Menu menu) {
  86.         // Inflate the menu; this adds items to the action bar if it is present.
  87.         getMenuInflater().inflate(R.menu.main, menu);
  88.         return true;
  89.     }*/
  90.  
  91.     @Override
  92.     public boolean onOptionsItemSelected(MenuItem item) {
  93.         // Handle action bar item clicks here. The action bar will
  94.         // automatically handle clicks on the Home/Up button, so long
  95.         // as you specify a parent activity in AndroidManifest.xml.
  96.         int id = item.getItemId();
  97.  
  98.         //noinspection SimplifiableIfStatement
  99.         if (id == R.id.action_settings) {
  100.             return true;
  101.         }
  102.  
  103.         return super.onOptionsItemSelected(item);
  104.     }
  105.  
  106.     @SuppressWarnings("StatementWithEmptyBody")
  107.     @Override
  108.     public boolean onNavigationItemSelected(MenuItem item) {
  109.         int id = item.getItemId();
  110.  
  111.         if (id == R.id.menu) {
  112.             Intent intent = new Intent(this, Menu.class);
  113.             Bundle bundle = new Bundle();
  114.             bundle.putSerializable("prodotti", (Serializable) prodotti);
  115.             intent.putExtras(bundle);
  116.             startActivity(intent);
  117.             //startActivity(new Intent(this,MenuList.class));
  118.         } else if (id == R.id.cart) {
  119.             startActivity(new Intent(MainActivity.this, ChoseList.class));
  120.         } else if (id == R.id.chose) {
  121.             startActivity(new Intent(MainActivity.this, ChoseList.class));
  122.         } else if (id == R.id.nav_share) {
  123.             Toast.makeText(this, "Non ancora implementata", Toast.LENGTH_LONG).show();
  124.         } else if (id == R.id.site) {
  125.             Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://fast-order.wixsite.com/fastorder"));
  126.             startActivity(intent);
  127.         } else if (id == R.id.accountLog) {
  128.             Toast.makeText(this, "Non ancora implementata", Toast.LENGTH_LONG).show();
  129.         }
  130.  
  131.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  132.         drawer.closeDrawer(GravityCompat.START);
  133.         return true;
  134.     }
  135.  
  136.     private void setConnectionTV() {
  137.         WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  138.         WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
  139.         String name = wifiInfo.getSSID();
  140.         reteTV.setText("Rete: " + name);
  141.  
  142.     }
  143.  
  144.     private void importData() {
  145.         /*WifiManager wifiMgr = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
  146.         String rete = wifiMgr.getConnectionInfo().getSSID();*/
  147.         //if (wifiMgr.getConnectionInfo().getSupplicantState().isValidState(wifiMgr.getConnectionInfo().getSupplicantState())) {
  148.  
  149.         try {
  150.             Class.forName("com.mysql.jdbc.Driver");
  151.         } catch (ClassNotFoundException e) {
  152.             Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
  153.         }
  154.         t1 = new Thread(new Runnable() {
  155.             public void run() {
  156.                 if (!Thread.interrupted()) {
  157.                     try {
  158.                         Thread.sleep(100);
  159.                     } catch (InterruptedException e1) {
  160.                     }
  161.  
  162.                     if (con == null) {
  163.                         try {
  164.                             con = DriverManager.getConnection("jdbc:mysql://mattone.ddns.net:3306/FastOrder", "root", "mattone17");
  165.                             System.out.println("connesso");
  166.                             //////////////
  167.                             try {
  168.                                 Statement stm = con.createStatement();
  169.                                 ResultSet rs = stm.executeQuery("SELECT * from Test");
  170.                                 //ResultSet rs=stm.executeQuery(query);
  171.                                 while (rs.next()) {
  172.                                     Prodotto p = new Prodotto(rs.getString("nome"), rs.getFloat("prezzo"));
  173.                                     prodotti.add(p);
  174.                                     System.out.println("Nome: " + rs.getString("nome"));
  175.                                 }
  176.                             } catch (SQLException e) {
  177.                                 System.out.println(e);
  178.                             }
  179.  
  180.                         } catch (SQLException e) {
  181.                             System.out.println(e);
  182.                             con = null;
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.         });//fine thread
  188.         if ((t1 != null) && (!t1.isAlive())) {
  189.             t1.start();
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement