0

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?

RunningWalks
  • 119
  • 2
  • 14
  • What are you trying to achieve with `String[] usereMail = getEmailsFromDB().split(",");;`?. Even if you get an array from `getEmailsFromDB` that wouldnt still compile. – Alexis C. Mar 31 '14 at 15:51
  • I was looking into using the split so that when selecting the emails from the database, it would place the comma between them therefore working like a regular email handler and sending them out like "iain@gmail.com, john@gmail.com, james@gmail.com". I understand now it is unnecessary. I havent worked with this before so was just confusing myself as i go along :) – RunningWalks Mar 31 '14 at 15:58

1 Answers1

4

Clearly the ArrayList<String> array and String[] are incompatible types

Use toArray to return an array of the type contained within the collection and match the expected return type for the method getEmailsFromDB

return list.toArray(new String[array.size()]);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • 2
    Np, consider using something like `list` as the variable name, `array` is bound to lead to confusion – Reimeus Mar 31 '14 at 15:53