Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

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?

share|improve this question
    
What are you trying to achieve with String[] usereMail = getEmailsFromDB().split(",");;?. Even if you get an array from getEmailsFromDB that wouldnt still compile. –  ZouZou yesterday
    
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 :) –  RunningWalks yesterday
add comment

marked as duplicate by Elenasys, njzk2, Gokul Nath, TGMCians, Greg 22 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 3 down vote accepted

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()]);
share|improve this answer
    
Thankyou so much :D –  RunningWalks yesterday
1  
Np, consider using something like list as the variable name, array is bound to lead to confusion –  Reimeus yesterday
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.