I am trying to send an email, from a button click, to all emails stored in a sqlite database. I have been successful in selecting one email, but now i am trying to use a cursor to continue to send the email to all stored email addresses. Below is the button call and the method to retrieve the array of addresses from the database.
view.findViewById(R.id.btn_save).setOnClickListener(new OnClickListener() {
public void onClick(View view ) {
Mail m = new Mail("gmail@gmail.com", "pw");
String[] usereMail = getEmailsFromDB().split(",");;
m.setTo(usereMail);
m.setFrom("iwalker77@gmail.com");
m.setSubject("Never going to happen");
m.setBody("If you receive this email, the planets have aligned and i have somehow managed to get an email sent to all players in the database");
try {
if(m.send()) {
Toast.makeText(getActivity(), "Email was sent successfully.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getActivity(), "Email was not sent.", Toast.LENGTH_LONG).show();
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
}
}
private ArrayList<String> getEmailsFromDB() {
// TODO Auto-generated method stub
dataBase = mHelper.getReadableDatabase();
Cursor Cursor = dataBase.rawQuery("SELECT " + DbHelper.KEY_EMAIL + " FROM "
+ DbHelper.TABLE_NAME, null);
ArrayList<String> array = new ArrayList<String>();
while(Cursor.moveToNext()) {
String usereMail = Cursor.getString(Cursor.getColumnIndex(DbHelper.KEY_EMAIL));
array.add(usereMail);
}
Cursor.close();
return array;
}
});
The error i am receiving is on the line ' String[] usereMail = getEmailsFromDB().split(",");' and it is due to the error 'Type mismatch: cannot convert from ArrayList to String[]'. Is there any way round this? And if not, how should i change my approach?