我正在尝试通过按钮点击发送电子邮件到存储在sqlite数据库中的所有电子邮件。我已成功选择一封电子邮件,但现在我正在尝试使用光标继续将电子邮件发送到所有存储的电子邮件地址。下面是按钮调用和从数据库中检索地址数组的方法。
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;
}
});
我收到的错误是在线上&#39; String [] usereMail = getEmailsFromDB()。split(&#34;,&#34;);&#39;这是由于错误&#39;类型不匹配:无法从ArrayList转换为String []&#39;。这有什么办法吗?如果没有,我该如何改变我的做法呢?
答案 0 :(得分:3)
显然ArrayList<String>
array
和String[]
是不兼容的类型
使用toArray
返回集合中包含的类型数组,并匹配方法getEmailsFromDB
的预期返回类型
return list.toArray(new String[array.size()]);