In my android app I'm trying to add several markers from sqlite to a
MapsActivity. I created a do-while cycle for getting all the cities in
sqlite table and convert all cities in coordinates (lat, long), but there's
something in error. Can you help me? Thanks to all
MapsActivity.java
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback {
private GoogleMap mMap;
DatabaseHelper myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to
be used.
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);}
@Overridepublic void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
LatLng ruvo = new LatLng(-45, 123);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in
Sydney"));
mMap.addMarker(new MarkerOptions().position(ruvo).title("Marker in
Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(ruvo));
displayCities();}
public void onSearch(View view) {
EditText textCity = (EditText) findViewById(R.id.textCity);
String location = textCity.getText().toString();
List addressList = null;
if (location != null) {
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(location, 1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(),
address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker
pointed"));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}
}
public void displayCities() {
List addressList = null;
Cursor database = myDb.display();
if (database.getCount()== 0) {
Toast.makeText(this, "Nothing to found", Toast.LENGTH_LONG).show();
return;
}
//string array of all cities
database.moveToFirst();
String[] cities = new String[database.getCount()];
int n = 0;
do {
cities[n] = database.getString(3).toString();
n=n+1;
} while (database.moveToNext());
Geocoder geocoder = new Geocoder(this);
try {
addressList = geocoder.getFromLocationName(cities[n], 1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(),
address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Marker
pointed"));
}
}
DataBaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Student.db";public static final
String TABLE_NAME = "student_table";//VERY IMPORTANT: CursorAdapter needs
exactly "_id" column, as written herepublic static final String COL_1 =
"_id";public static final String COL_2 = "NAME";public static final String
COL_3 = "SURNAME";public static final String COL_4 = "CITY";
public ArrayList results = new ArrayList<>();
public DatabaseHelper(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);}
@Overridepublic void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " ( " + COL_1 + " INTEGER PRIMARY
KEY AUTOINCREMENT , "
+ COL_2 + " TEXT , " + COL_3 + " TEXT , " + COL_4 + " TEXT );");}
@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);}
public Cursor selected(long id) {
SQLiteDatabase db = this.getWritableDatabase();
String[] columns = new String[]{DatabaseHelper.COL_1, DatabaseHelper.COL_2,
DatabaseHelper.COL_3, DatabaseHelper.COL_4};
Cursor c = db.query(DatabaseHelper.TABLE_NAME, columns,
DatabaseHelper.COL_1 + "=" + id, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;}
public boolean insertData(String name, String surname, String city) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues ContentValues = new ContentValues();
ContentValues.put(COL_2, name);
ContentValues.put(COL_3, surname);
ContentValues.put(COL_4, city);
long result = db.insert(TABLE_NAME, null, ContentValues);
if (result == -1)
return false;
else
return true;}
//method for select querypublic Cursor display() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("select * from " +