SHARE
TWEET

Untitled

a guest Jul 15th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.ujicobaspinner;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8.  
  9. import android.annotation.SuppressLint;
  10. import android.os.Bundle;
  11. import android.os.StrictMode;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.widget.AdapterView;
  15. import android.widget.AdapterView.OnItemSelectedListener;
  16. import android.widget.ArrayAdapter;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.Spinner;
  20. import android.widget.Toast;
  21.  
  22. import androidx.appcompat.app.AppCompatActivity;
  23.  
  24.  
  25. public class MainActivity extends AppCompatActivity {
  26.     Spinner spinner1, spinner2, spinner3;
  27.     String ip, db, un, passwords;
  28.     Button btnsearch;
  29.     EditText searchdata;
  30.     EditText edtnobarang;
  31.     Connection connect;
  32.     PreparedStatement stmt;
  33.     ResultSet rs;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.         ip = "192.168.0.171";
  40.         un = "sa";
  41.         passwords = "arya281196";
  42.         db = "aryaaa";
  43.  
  44.         btnsearch = (Button) findViewById(R.id.btnsearch);
  45.         searchdata  = (EditText) findViewById(R.id.searchdata);
  46.         edtnobarang = (EditText) findViewById(R.id.edtnobarang);
  47.         spinner1 = (Spinner) findViewById(R.id.idspinner1);
  48.         spinner2 = (Spinner) findViewById(R.id.idspinner2);
  49.         //spinner3 = (Spinner) findViewById(R.id.idspinner3);
  50.  
  51.  
  52.         btnsearch.setOnClickListener(new View.OnClickListener() {
  53.             @Override
  54.             public void onClick(View v) {
  55.                 String search =  searchdata.getText().toString().trim();
  56.                 setSpinner1 (search);
  57.                 setSpinner2 (search);
  58.             }
  59.         });
  60.  
  61.         edtnobarang.setOnFocusChangeListener(new View.OnFocusChangeListener() {
  62.  
  63.             @Override
  64.             public void onFocusChange(View arg0, boolean hasFocus) {
  65.                 // TODO Auto-generated method stub
  66.                 if(hasFocus){
  67.                     spinner2.setVisibility(View.VISIBLE);
  68.  
  69.                 }
  70.             }
  71.         });
  72.  
  73.         spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
  74.  
  75.             @Override
  76.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  77.                 String name = spinner1.getSelectedItem().toString();
  78.                 if(name.equals("Pintu")){
  79.                 }
  80.  
  81.                 Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
  82.             }
  83.             @Override
  84.             public void onNothingSelected(AdapterView<?> parent) {
  85.             }
  86.         });
  87.  
  88.  
  89.         spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
  90.  
  91.             @Override
  92.             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  93.                 String name = spinner2.getSelectedItem().toString();
  94.                 if (name.equals("")){
  95.  
  96.                 }
  97.                 Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
  98.             }
  99.             @Override
  100.             public void onNothingSelected(AdapterView<?> parent) {
  101.             }
  102.         });
  103.  
  104. //        spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
  105. //            @Override
  106. //            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  107. //                String name = spinner3.getSelectedItem().toString();
  108. //                Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
  109. //            }
  110. //            @Override
  111. //            public void onNothingSelected(AdapterView<?> parent) {
  112. //            }
  113. //        });
  114.     }
  115.  
  116.     // set spinner pertama (1)
  117.     public void setSpinner1(String kode) {
  118.         connect = CONN(un, passwords, db, ip);
  119.         String query1 = "select namabarang from Jenis_Barang where id = '"+kode+"' ";
  120.         Log.e("query",""+query1);
  121.         try {
  122.             connect = CONN(un, passwords, db, ip);
  123.             stmt = connect.prepareStatement(query1);
  124.             rs = stmt.executeQuery();
  125.  
  126.             ArrayList<String> data = new ArrayList<String>();
  127.             while (rs.next()) {
  128.                 String id = rs.getString(1);
  129.                 data.add(id);
  130.                 Log.e("idun",""+id);
  131.             }
  132.             String[] array = data.toArray(new String[0]);
  133.             ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
  134.             spinner1.setAdapter(NoCoreAdapter);
  135.         } catch (Exception e) {
  136.             e.printStackTrace();
  137.             Log.e("errora",""+e.getMessage());
  138.         }
  139.  
  140.     }
  141.  
  142.  
  143.     //set spinner ke dua
  144.     public void setSpinner2(String kode) {
  145.         connect = CONN(un, passwords, db, ip);
  146.         String query2 = "select namaitembarang from Item_Barang where id = '"+kode+"' ";
  147.         Log.e("query",""+query2);
  148.         try {
  149.             connect = CONN(un, passwords, db, ip);
  150.             stmt = connect.prepareStatement(query2);
  151.             rs = stmt.executeQuery();
  152.  
  153.             ArrayList<String> data = new ArrayList<String>();
  154.             while (rs.next()) {
  155.                 String id = rs.getString(1);
  156.                 data.add(id);
  157.             }
  158.  
  159.             String[] array = data.toArray(new String[0]);
  160.             ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
  161.             spinner2.setAdapter(NoCoreAdapter);
  162.         } catch (Exception e) {
  163.             e.printStackTrace();
  164.         }
  165.     }
  166.    
  167.  
  168.  
  169.  
  170.  
  171.     @SuppressLint("NewApi")
  172.     private Connection CONN(String _user, String _pass, String _DB, String _server) {
  173.         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  174.         StrictMode.setThreadPolicy(policy);
  175.         Connection conn = null;
  176.         String ConnURL = null;
  177.         try {
  178.             Class.forName("net.sourceforge.jtds.jdbc.Driver");
  179.             ConnURL = "jdbc:jtds:sqlserver://" + _server + ";"
  180.                     + "databaseName=" + _DB + ";user=" + _user + ";password="
  181.                     + _pass + ";";
  182.             conn = DriverManager.getConnection(ConnURL);
  183.         } catch (SQLException se) {
  184.             Log.e("ERRO", se.getMessage());
  185.         } catch (ClassNotFoundException e) {
  186.             Log.e("ERRO", e.getMessage());
  187.         } catch (Exception e) {
  188.             Log.e("ERRO", e.getMessage());
  189.         }
  190.         return conn;
  191.     }
  192. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top