2015-01-21 37 views
0

我需要發送一些數據作爲電子郵件按鈕click.I成功添加數據從編輯text.But我不知道如何發送數據從我的數據庫中的數組。代碼我需要發送PN [I] .I附加值的值等this.but它在這一行Android-如何發送字符串數組作爲電子郵件正文

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

代碼引起錯誤:

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(); 

     } 
    }); 
+0

你想發送數組的所有條目還是僅發送特定條目? – 2015-01-21 11:35:42

+0

我想發送數組的所有條目。 – paypaluser 2015-01-21 11:40:36

+0

好吧,所以你將不得不將所有數組項合併成一個字符串。你想在項目之間分隔嗎?也許是空格字符或逗號? – 2015-01-21 11:46:08

回答

2

方法一:(內建方法)

String productNames = Arrays.toString(pn); 

方法二:(在新行每個產品)

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

方法三:(編號列表,在新行每個產品)

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

它工作完美:)謝謝 – paypaluser 2015-01-21 12:05:21

相關問題