1

I have a question. How can I share all items in an array using Intent to be shared in the Java language for Android? Because I have many items in the Array, in the example below I only put 2, but there are dozens.

Here's my code:

    String[] pe_Contra = {
            "Adalberto Cavalcanti",
            "Augusto Coutinho",
    };

    String[] pe_Contra_partido = {
            "PTB",
            "SD",
    };

    String[] pe_Contra_ComoVotou = {
            "Contra a Denúncia.",
            "Contra a Denúncia.",
        };


 botaoCompartilhar.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, pe_Contra, pe_Contra_partido,  pe_Contra_ComoVotou);
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        });

Thank you so much!!!

  • please check this https://stackoverflow.com/questions/4429036/passing-string-array-between-android-activities – vinod Sep 17 '17 at 15:59
  • Possible duplicate of [Passing string array between android activities](https://stackoverflow.com/questions/4429036/passing-string-array-between-android-activities) – ישו אוהב אותך Sep 17 '17 at 16:37

3 Answers3

2

try this use Bundle to pass String array with intent

use below code to send String array

Bundle b=new Bundle();
b.putStringArray("data1", pe_Contra);
b.putStringArray("data2", pe_Contra_partido);
b.putStringArray("data3", pe_Contra_ComoVotou);
Intent sendIntent=new Intent(this, OTherActivity.Class);
sendIntent.putExtras(b);
startActivity(sendIntent);

to receive it in other activity use this

Bundle b=this.getIntent().getExtras();
String[] array1=b.getStringArray("data1");
String[] array2=b.getStringArray("data2");
String[] array3=b.getStringArray("data3");
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
1

Pass arrays one by one using different keys as below:

sendIntent.putExtra("data1", pe_Contra);
sendIntent.putExtra("data2", pe_Contra_partido);
sendIntent.putExtra("data3", pe_Contra_ComoVotou);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
1

Use this to know how to put more than 1 extra with Bundle or just with many putExtras: Put 2 extras in intent and make one of the bundle items a count to know what is the size of the array. use a loop to build and read your Bundle according to the array size.