如何通过按钮将字符串数组从一个活动传递到另一个活动?

时间:2017-05-03 04:16:09

标签: java android

String[] charc={"FLash","ARROW","SUPERGIRL","BATMAN","SUPERMAN","ATOM","AQUAMAN","CYBORG","JOKER","BANE","GREENLANTERN","ZOOM","REVERSEFLASH","FIRESTORM"};
    private String s;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(getApplicationContext(),"onCreate was Called",Toast.LENGTH_SHORT).show();
        lv = (ListView) findViewById(R.id.idList);
        b = (Button) findViewById(R.id.Btn);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,charc);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {

                        s=charc[i];
                b.setText(s);

                Toast.makeText(getApplicationContext(),"Item Clicked: "+i,Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void btnclick(View v){

        startActivity(new Intent(MainActivity.this, approutes.list.Display.class)
                .putExtra("Flash",s));

    }

4 个答案:

答案 0 :(得分:3)

意图接受字符串数组

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("string", array);
startActivity(intent);

在您的接收意图中,您需要:

Intent i = getIntent();  
string_array = i.getStringArrayListExtra("string");

答案 1 :(得分:0)

试试这个。

来自您的第一个活动

Intent intent = new Intent(context, FirstActivity.class);
intent.putExtra("string-array", charc);
context.startActivity(intent);

然后在下一个活动中在Create()

上获取它
Intent intent = getIntent();
String [] stringArray = intent.getStringArrayExtra("string-array");

答案 2 :(得分:0)

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("list_as_string", charc);
startActivity(intent);

除此之外,你应该将Manifest中的SecondActivity作为Activity添加,并确保你已经在xml布局中添加了btnclick()方法按钮,或者你可以设置按钮点击以下

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // call intent here
            }
        });

答案 3 :(得分:0)

要在活动之间传递数据,有Intent个对象。你应该这样做:

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("charc", charc);
context.startActivity(intent);

并在charc中获取NextActivity

Intent intent = getIntent();
    char[] charc = intent.getStringExtra("charc");