I am fairly new to android programming and I have faced a small problem. I have an arraylist consisting of names of person selected from a multi-select listview, the problem is that Whenever I insert those arraylist values into the database, It inserts it as one string: Database row. How do i iterate thru an arraylist and at the same time insert its values into the database?
here is my code:
try {
Connection con = connectionClass.CONN();
if (con == null) {
Toast.makeText(getApplicationContext(), "CONNECTION FAIL", Toast.LENGTH_LONG).show();
} else {
String samp = "";
String names = "";
samp = myArrayAdapter.getCheckedItems().toString();
ArrayList<String> data1 = new ArrayList<String>();
data1.add(samp);
for(int x=0; x<data1.size(); x++)
{
names += String.valueOf(data1.get(x));
String query = "INSERT INTO AUTOINC(PersonName)"+"VALUES('"+names+"')";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.executeUpdate();
}
Toast.makeText(getApplicationContext(), "INSERT SUCCESS", Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), "INSERT FAILED", Toast.LENGTH_LONG).show();
Log.e("MYAPP", "exception", ex);
}
Thank you for any future replies.
names += String.valueOf(data1.get(x))? The plus sign on this line will concatenate one name each iteration and on the last iteration it will add a new line with all names concatenated.