简体   繁体   中英

String array pass to another activity - android studio

I am trying to pass a string array to another activity with using intent; here are my intent codes:

private void activityGecis2() {
    Intent gecis2 = new Intent(this, dizilisActivity.class);
    gecis2.putExtra("isimler",oyuncular);
    startActivity(gecis2);

When I passed them I want to set them as textview text. but gelenIsimler returns null. So here are codes so what should I do?

public class dizilisActivity extends AppCompatActivity {
    TextView oyuncu1, oyuncu2,oyuncu3,oyuncu4,oyuncu5,oyuncu6,oyuncu7,oyuncu8,oyuncu9,oyuncu10;
    TextView[] oyuncular = {oyuncu1, oyuncu2,oyuncu3,oyuncu4,oyuncu5,oyuncu6,oyuncu7,oyuncu8,oyuncu9,oyuncu10};
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dizilis);
        String[] gelenIsimler = getIntent().getStringArrayExtra("isimler");


        oyuncu1 = (TextView) findViewById(R.id.oyuncu1);
        oyuncu2 = (TextView) findViewById(R.id.oyuncu2);
        oyuncu3= (TextView) findViewById(R.id.oyuncu3);
        oyuncu4= (TextView) findViewById(R.id.oyuncu4);
        oyuncu5= (TextView) findViewById(R.id.oyuncu5);
        oyuncu6 = (TextView) findViewById(R.id.oyuncu6);
        oyuncu7 = (TextView) findViewById(R.id.oyuncu7);
        oyuncu8 = (TextView) findViewById(R.id.oyuncu8);
        oyuncu9 = (TextView) findViewById(R.id.oyuncu9);
        oyuncu10 = (TextView) findViewById(R.id.oyuncu10);

for (int i = 0; i < oyuncular.length; i++ )
    oyuncular[i].setText(gelenIsimler[i]);

    }
}

You have to get the series sent with the Bundle .

Bundles are generally used for passing data between various Android activities. It depends on you what type of values you want to pass, but bundles can hold all types of values and pass them to the new activity.

Bundle extras =  getIntent().getExtras();
String[] gelenIsimler = extras.getStringArrayExtra("isimler");

Solution - 2

Bundle b=new Bundle();
b.putStringArray("isimler", oyuncular);
Intent intent=new Intent(this, dizilisActivity.class);
intent.putExtras(b);
startActivity(intent);

Other Activity

 Intent intent=getIntent();
 Bundle b=intent.getExtras();
 String[] array=b.getStringArray("isimler");

Change your code like below

private void activityGecis2() {
    Intent gecis2 = new Intent(this, dizilisActivity.class);
    Bundle bundle = new Bundle();
    bundle.putStringArray("isimler", oyuncular);
    gecis2.putExtras(bundle);
    startActivity(gecis2);
}

In dizilisActivity get the array like below

String[] gelenIsimler = getIntent().getExtras().getStringArray("isimler");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM