patrick1980 patrick1980 - 1 month ago 7
Java Question

Try to convert Arraylist<LatLng> to string before insert to database

I try to insert my

ArrayList<LatLng> list1
with a lot of values like this:
(99.9999999,99.9999999)
to table in database MySQL, exacly to 1 column, something like this:

row 1 (99.9999999,99.9999999)
row 2 (99.9999999,99.9999999)
row 3 (99.9999999,99.9999999)


... all to 1 column.

In my opinion, currently i have a good method for this:

String sql = "INSERT INTO table1 VALUES(";
for(String s : list1) {
sql = sql+"'"+s+"'";
}
sql = sql+")";
stmt.executeUpdate(sql);


but Android Studio underlines
String s
and says:

Incompatible types
Required: com.google.android.gms.maps.model.LatLng
Found: java.lang.String


In my opinion, Android Studio trying to say me: you need to convert all values from
ArrayList<LatLng> list1
to
String
!

Is it possible to convert all values from my
ArrayList
in one method ?

Answer

You can convert your data to string the following way:

for(LatLng s : list1)
{
    String sql = "INSERT INTO table1 VALUES('"+s+"');
    stmt.executeUpdate(sql);
}

That is, you don't have to do anything specific to convert it. I'm assuming you have the method toString() implemented in your LatLng class to give objects of LatLng type a meaningful string representation.