I have a sqlite database
for saving name
, address
, category
, latitude
andlongitude
. In activity(1) i construct a listview
. I have been able to retrieve data name for show in the listview, If the list is clicked it will open activity(2) which will showing data name, address, category and at the bottom there a map
. I have been able to retrieve name, address and category in Activity(2) but i am confused to retrieve latitude and longitude for use to put marker in the map
.
This my database class:
public class DB_Restoran extends SQLiteOpenHelper {final static String DB_NAME = "db_restoran";public DB_Restoran(Context context) { super(context, DB_NAME, null, 1); //Todo auto}@Overridepublic void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS restoran(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, category TEXT, address TEXT, latitude DOUBLE, longitude DOUBLE)"; db.execSQL(sql); ContentValues values = new ContentValues(); values.put("_id", "1"); values.put("name", "RM. Kemang Raya"); values.put("category", "Umum"); values.put("address", "Jl.Endro Suratmin"); values.put("latitude", "-5.384402"); values.put("longitude", "105.295443"); db.insert("restoran", "_id", values); values.put("_id", "2"); values.put("nama", "RM. Dua Saudara"); values.put("category", "Masakan Padang"); values.put("address", "Jl.P.Tirtayasa Sukabumi"); values.put("latitude", "-5.384402"); values.put("longitude", "105.295443"); db.insert("restoran", "_id", values); values.put("_id", "3"); values.put("name", "RM. Mbok Wito"); values.put("category", "Umum"); values.put("address", "Jl.Arief Rahman Hakim"); values.put("latitude", "-5.384402"); values.put("longitude", "105.295443"); db.insert("restoran", "_id", values); values.put("_id", "4"); values.put("name", "Babe Cafe"); values.put("category", "Cafe"); values.put("address", "Jl.Arief Rahman Hakim"); values.put("latitude", "-5.384402"); values.put("longitude", "105.295443"); db.insert("restoran", "_id", values); values.put("_id", "5"); values.put("name", "KFC Coffe"); values.put("category", "Cepat Saji"); values.put("address", "Jl.Zaenal Abidin Pagar Alam"); values.put("latitude", "-5.384402"); values.put("longitude", "105.295443"); db.insert("restoran", "_id", values);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS restoran"); onCreate(db);}}
This my Listview class(Activity1):
public class Menu_Restoran extends ActionBarActivity {protected ListView lv;protected ListAdapter adapter; SQLiteDatabase db;Cursor cursor;@SuppressWarnings("deprecation")@Overrideprotected void onCreate(Bundle SavedInstanceState) { super.onCreate(SavedInstanceState); setContentView(R.layout.menu_restoran); // enable up/back button getSupportActionBar().setDisplayHomeAsUpEnabled(true); db = (new DB_Restoran(this)).getWritableDatabase(); lv = (ListView) findViewById(R.id.list); try { cursor = db.rawQuery("SELECT * FROM restoran ORDER BY name ASC", null); adapter = new SimpleCursorAdapter(this, R.layout.list_view, cursor, new String[]{"name", "category", "address", "latitude", "longitude"}, new int[]{R.id.item, R.id.textView1, R.id.textView2, R.id.map}); lv.setAdapter(adapter); lv.setTextFilterEnabled(true); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { detail(position); } }); } catch (Exception e) { e.printStackTrace(); }} public void detail(int position) { Double latitude = 0; Double longitude = 0; String _id = ""; String name = ""; String category = ""; String address = ""; if (cursor.moveToFirst()) { cursor.moveToPosition(position); name = cursor.getString(cursor.getColumnIndex("name")); category = cursor.getString(cursor.getColumnIndex("category")); address = cursor.getString(cursor.getColumnIndex("address")); latitude = cursor.getDouble(cursor.getColumnIndex("latitude")); longitude = cursor.getDouble(cursor.getColumnIndex("longitude")); } Intent iIntent = new Intent(this, DBResto_Parse.class); iIntent.putExtra("dataName", name); iIntent.putExtra("dataCategory", category); iIntent.putExtra("dataAddress", address); iIntent.putExtra("dataLatitude", latitude); iIntent.putExtra("dataLongitude", longitude); setResult(RESULT_OK, iIntent); startActivityForResult(iIntent, 99); } }
This my parse class(Activity2):
public class DBResto_Parse extends ActionBarActivity {TextView tv_name, tv_category, tv_address, id; Double latitude;Double longitude;private GoogleMap mMap; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.restoran); FragmentManager myFragmentManager = getSupportFragmentManager(); SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map); mMap = mySupportMapFragment.getMap(); // enable up/back button getSupportActionBar().setDisplayHomeAsUpEnabled(true); // create marker final MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Kfc").snippet("Fast Food").flat(true); // Changing marker icon marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_resto)); // adding marker mMap.addMarker(marker); // Construct a CameraPosition focusing on Mountain View and animate the camera to that position. CameraPosition cameraPosition = new CameraPosition.Builder() .target(new LatLng(latitude, longitude)) // Sets the center of the map to Mountain View .zoom(17) // Sets the zoom .bearing(90) // Sets the orientation of the camera to east .tilt(30) // Sets the tilt of the camera to 30 degrees .build(); // Creates a CameraPosition from the builder mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); Intent iIdentifikasi = getIntent(); latitude = iIdentifikasi.getDoubleExtra("dataLat", 0); longitude = iIdentifikasi.getDoubleExtra("dataLng", 0); String msg_name = iIdentifikasi.getStringExtra("dataName"); String msg_category = iIdentifikasi.getStringExtra("dataCategory"); final String msg_address = iIdentifikasi.getStringExtra("dataAdress"); tv_name = (TextView) findViewById(R.id.tvName); tv_category = (TextView) findViewById(R.id.tvCategory); tv_address = (TextView) findViewById(R.id.tvAddress); tv_name.setText(msg_name); tv_category.setText(msg_category); tv_address.setText(msg_address);}}
Have you guys can tell me to do this? Thanks in advance