user2600853 user2600853 - 24 days ago 6
Android Question

How to send an ArrayList in the body of an e-mail in android?

I want to send in the body of the e-mail an ArrayList with some values.
Like this:

Button bt_send = (Button) findViewById(R.id.bt_cadastrar_medicos);
bt_cadastrar_medicos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, "email@email");
intent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
ArrayList<String> array = new ArrayList<String>();
for(int i=0; i<2; i++){
String ii = String.valueOf(i);
niveis.add(ii);
niveis.add(ii);
intent.putExtra(Intent.EXTRA_TEXT, niveis.get(i));
}

startActivity(Intent.createChooser(intent, "Choose the app to send:"));
}
});


This is just an example, actually I want to add in which array position values that a get from a java class. But in the body just the last value appears, in this case would be the "2".

How do I show all of them in the body? Is that possible?

Thank You.

Answer

In your for-loop, this call

intent.putExtra(Intent.EXTRA_TEXT, niveis.get(i));

is replacing the EXTRA_TEXT data on each pass through the loop. You need to build the entire string that you want to send as the EXTRA_TEXT, then add that to the intent via a single call to putExtra(). E.g.

StringBuilder sb = new StringBuilder(); 
for (String s : myArrayList) {
    sb.append(s);
    sb.append("\n");
}
intent.putExtra(Intent.EXTRA_TEXT, sb.toString());