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("[email protected]", "pw"); 



          String[] usereMail = getEmailsFromDB().split(",");;
          m.setTo(usereMail); 
          m.setFrom("[email protected]"); 
          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?

2
  • What are you trying to achieve with String[] usereMail = getEmailsFromDB().split(",");;?. Even if you get an array from getEmailsFromDB that wouldnt still compile. Commented Mar 31, 2014 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 "[email protected], [email protected], [email protected]". I understand now it is unnecessary. I havent worked with this before so was just confusing myself as i go along :) Commented Mar 31, 2014 at 15:58

1 Answer 1

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()]);
Sign up to request clarification or add additional context in comments.

1 Comment

Np, consider using something like list as the variable name, array is bound to lead to confusion

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.