将字符串数组传递给另一个活动 – Android Studio

huangapple 未分类评论52阅读模式
英文:

String array pass to another activity - android studio

问题

下面是翻译好的部分:

private void activityGecis2() {
    Intent gecis2 = new Intent(this, dizilisActivity.class);
    gecis2.putExtra("isimler", oyuncular);
    startActivity(gecis2);
}
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]);
    }
}

请注意,翻译可能会因格式问题而产生细微差异。如果您在代码中遇到问题,请随时提问。

英文:

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(&quot;isimler&quot;,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(&quot;isimler&quot;);


        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 &lt; oyuncular.length; i++ )
    oyuncular[i].setText(gelenIsimler[i]);

    }
}

答案1

得分: 0

你需要获得通过**Bundle**发送的系列数据。

&gt; Bundle通常用于在不同的Android活动之间传递数据。你可以决定要传递哪种类型的值,但Bundle可以保存各种类型的值并将它们传递给新的活动。


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


解决方案 - 2

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


**其他活动**

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

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(&quot;isimler&quot;);

Solution - 2

Bundle b=new Bundle();
b.putStringArray(&quot;isimler&quot;, 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(&quot;isimler&quot;);

答案2

得分: 0

更改您的代码如下:

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

dizilisActivity中获取数组如下:

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

Change your code like below

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

In dizilisActivity get the array like below

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

huangapple
  • 本文由 发表于 2020年4月5日 00:28:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61031190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定