gpt4 book ai didi

java - 无法从 ArrayList 转换为 String[]

转载 作者:行者123 更新时间:2023-12-01 18:34:11 24 4
gpt4 key购买 nike

我正在尝试通过单击按钮向存储在 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;

}
});

我收到的错误位于“String[] usereMail = getEmailsFromDB().split(",");”行这是由于错误“类型不匹配:无法从 ArrayList 转换为 String[]”。有什么办法解决这个问题吗?如果不是,我应该如何改变我的方法?

最佳答案

显然是ArrayList<String> arrayString[]是不兼容的类型

使用 toArray 返回集合中包含的类型的数组并匹配方法 getEmailsFromDB 的预期返回类型

return list.toArray(new String[array.size()]);

关于java - 无法从 ArrayList<String> 转换为 String[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22766116/

24 4 0