khirulnizam

LokasiMenarik

Jan 8th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. //package my.edu.kuis.lokasimenarik;
  2.  
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10.  
  11. public class CarianLokasi extends AppCompatActivity
  12.         implements View.OnClickListener{
  13.     EditText txtlokasi, txtlatitude, txtlongitude;
  14.     Button btncari;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_carian_lokasi);
  19.  
  20.         //UI object
  21.         txtlokasi=(EditText)findViewById(R.id.txtlokasi);
  22.         txtlatitude=(EditText)findViewById(R.id.txtlatitude);
  23.         txtlongitude=(EditText)findViewById(R.id.txtlongitude);
  24.         //btn
  25.         btncari=(Button) findViewById(R.id.btncari);
  26.         btncari.setOnClickListener(this);
  27.     }//end onCreate
  28.  
  29.     private MyDB mydb = new MyDB(this);
  30.     private SQLiteDatabase db;//the connector
  31.  
  32.     public void onClick(View v){
  33.         //carian berlaku di sini
  34.         String carian = txtlokasi.getText().toString();
  35.  
  36.         //start searching
  37.         db = mydb.getReadableDatabase();
  38.         String sql = "SELECT * FROM lokasi where " +
  39.                 "namalokasi LIKE '%"+ carian +"%' ";
  40.         //simpan hasil carian dalam cursor
  41.         Cursor hasil=db.rawQuery(sql, null);
  42.         //kalau carian jumpa
  43.         if (hasil.getCount()!=0){
  44.             hasil.moveToFirst();//point to first row
  45.             txtlokasi.setText(hasil.getString(0));
  46.             txtlatitude.setText(hasil.getString(1));
  47.             txtlongitude.setText(hasil.getString(2));
  48.         }
  49.         else {
  50.             txtlokasi.setText("Carian tak jumpa");
  51.             txtlatitude.setText("");
  52.             txtlongitude.setText("");
  53.         }
  54.     }//end onClick
  55. }
Advertisement
Add Comment
Please, Sign In to add comment