Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package my.edu.kuis.lokasimenarik;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class CarianLokasi extends AppCompatActivity
- implements View.OnClickListener{
- EditText txtlokasi, txtlatitude, txtlongitude;
- Button btncari;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_carian_lokasi);
- //UI object
- txtlokasi=(EditText)findViewById(R.id.txtlokasi);
- txtlatitude=(EditText)findViewById(R.id.txtlatitude);
- txtlongitude=(EditText)findViewById(R.id.txtlongitude);
- //btn
- btncari=(Button) findViewById(R.id.btncari);
- btncari.setOnClickListener(this);
- }//end onCreate
- private MyDB mydb = new MyDB(this);
- private SQLiteDatabase db;//the connector
- public void onClick(View v){
- //carian berlaku di sini
- String carian = txtlokasi.getText().toString();
- //start searching
- db = mydb.getReadableDatabase();
- String sql = "SELECT * FROM lokasi where " +
- "namalokasi LIKE '%"+ carian +"%' ";
- //simpan hasil carian dalam cursor
- Cursor hasil=db.rawQuery(sql, null);
- //kalau carian jumpa
- if (hasil.getCount()!=0){
- hasil.moveToFirst();//point to first row
- txtlokasi.setText(hasil.getString(0));
- txtlatitude.setText(hasil.getString(1));
- txtlongitude.setText(hasil.getString(2));
- }
- else {
- txtlokasi.setText("Carian tak jumpa");
- txtlatitude.setText("");
- txtlongitude.setText("");
- }
- }//end onClick
- }
Advertisement
Add Comment
Please, Sign In to add comment