将列表<string>传输到另一个活动ListView

时间:2017-11-14 12:26:29

标签: java android

MainActivity:

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.*;
import android.content.Intent;

public class MainActivity extends Activity {

    private Spinner spinner1;
    private Button btnSubmit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
        addListenerOnSpinnerItemSelection();
    }

    public void addListenerOnSpinnerItemSelection() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
    }

    // get the selected dropdown list value
    public void addListenerOnButton() {

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
        btnSubmit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //Get Value from EditText                    //Save value to a Variable
                EditText person0 = findViewById(R.id.spot0); String saved_spot0 = person0.getText().toString();
                EditText person1 = findViewById(R.id.spot1); String saved_spot1 = person1.getText().toString();
                EditText person2 = findViewById(R.id.spot2); String saved_spot2 = person2.getText().toString();
                EditText person3 = findViewById(R.id.spot3); String saved_spot3 = person3.getText().toString();
                EditText person4 = findViewById(R.id.spot4); String saved_spot4 = person4.getText().toString();
                EditText person5 = findViewById(R.id.spot5); String saved_spot5 = person5.getText().toString();
                EditText person6 = findViewById(R.id.spot6); String saved_spot6 = person6.getText().toString();
                EditText person7 = findViewById(R.id.spot7); String saved_spot7 = person7.getText().toString();
                EditText person8 = findViewById(R.id.spot8); String saved_spot8 = person8.getText().toString();
                EditText person9 = findViewById(R.id.spot9); String saved_spot9 = person9.getText().toString();

                List<String> filled_spots = Arrays.asList(saved_spot0, saved_spot1, saved_spot2, saved_spot3, saved_spot4, saved_spot5, saved_spot6, saved_spot7, saved_spot8, saved_spot9);

                Collections.shuffle(filled_spots); //Shuffle List
                Collections.sort(filled_spots); //Sort List

                Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
                teams_Screen.putStringArrayListExtra ("mylist", (ArrayList<String>) filled_spots);

                startActivity(teams_Screen);
            }

        });
    }
}

DisplayTeamsActivity:

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class DisplayTeamsActivity extends AppCompatActivity {

    private ListView lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_teams);


        ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");

    }
}

我需要将List数组从一个活动传递到另一个活动并填充我从shuffeld数组中获得的listView,但当我点击导致我进入下一个活动的按钮时,某些东西会崩溃我的应用程序。我不知道这是否正确完成,导致它完全崩溃的问题是什么......

2 个答案:

答案 0 :(得分:0)

你可以试试这个

in first acticity

  

Intent intent = new Intent(context,NewActivity.class);

     

intent.putExtra(&#34; mylist&#34;,yourlist_array);

     

context.startActivity(意向);

在第二项活动中

  

Intent intent = getIntent();

     

列出stringArray = intent.getStringArrayExtra(&#34; mylist&#34;);

答案 1 :(得分:0)

这样做:

ArrayList<String> filled_spots = new ArrayList<>(Arrays.asList("nome1", "nome2"));

Collections.shuffle(filled_spots); //Shuffle List
Collections.sort(filled_spots); //Sort List

Intent teams_Screen = new Intent (MainActivity.this, DisplayTeamsActivity.class);
teams_Screen.putStringArrayListExtra("mylist", filled_spots);

在DisplayTeamsActivity,你需要这样做:

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ArrayList<String> stringList = getIntent().getStringArrayListExtra("mylist");
    }