panda_heart_3
panda_heart_3

Reputation: 227

Android- How to send string array as email body

I need to send some data as email on button click.I successfully added data from edit text.But I donot know how to send an array of data from my database.In my code I need to send values of pn[i].I added the value like this.but it caused error in this line

emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"products:"+pn[i]);

Code:

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mydb=buy_ltr.this.openOrCreateDatabase("addcart", MODE_PRIVATE, null);
            Cursor cr = mydb.rawQuery("SELECT * FROM add2cart WHERE usr='"+cont+"'", null);
            String [] pn = new String[cr.getCount()];

            int i = 0;
            while(cr.moveToNext())
            {

                String prp = cr.getString(cr.getColumnIndex("prate"));
                pn[i] = prp;
                i++;
            }


              Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
              String[] recipients = new String[]{"[email protected]", "",};
              emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
              emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,"name:"+edt1.getText().toString()+"\n"+"address:"+edt2.getText().toString()+"\n"+"products:"+pn[i]);
              emailIntent.setType("text/plain");
    //        emailIntent.setType("message/rfc822");
              startActivity(Intent.createChooser(emailIntent, "Send mail..."));
              finish();

        }
    });

Upvotes: 0

Views: 1092

Answers (1)

Rolf ツ
Rolf ツ

Reputation: 8781

Method one: (Build in method)

String productNames = Arrays.toString(pn);

Method two: (Each product on a new line)

StringBuilder productNamesBuilder = new StringBuilder();
for(String productName: pn){
    productNamesBuilder.append(productName + "\n");
}
String productNames = productNamesBuilder.toString();

Method three: (Numbered list, each product on a new line)

StringBuilder productNamesBuilder = new StringBuilder();
for(int i = 0; i < pn.length; i++){
    productNamesBuilder.append((i + 1) + " " + pn[i] + "\n");
}
String productNames = productNamesBuilder.toString();

Upvotes: 2

Related Questions