在另一个活动中使用数组

时间:2014-12-08 13:01:10

标签: android arrays

Android新手,是否有可能在一个活动中创建一个String数组并传递该数组的内容,所以我可以在另一个活动中使用它?如果是这样的话?

以下是我的第一项活动。

    public class Screen2 extends MyActivity implements TextWatcher, View.OnClickListener { EditText quizNameText;
 EditText quesText;
 EditText correctAnsText;
 EditText hintText;
 int index=0;
 Button saveAndNext;
 protected String yourQuizName;
 public String[]
    question = new String[3];
 public String[] answer = new String[3];
 public String[] hint = new String[3];
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
 setContentView(R.layout.screen2);
 quizNameText = (EditText)
    findViewById(R.id.enterquiztitle);
 quesText = (EditText) findViewById(R.id.enterqueshere);
 correctAnsText = (EditText) findViewById(R.id.entercorrectans);
 hintText = (EditText) findViewById(R.id.hintText);
 quizNameText.setText(yourQuizName);
 quesText.setText(question[0]);

    correctAnsText.setText(answer[0]);
 hintText.setText(hint[0]);
 saveAndNext = (Button) findViewById(R.id.yourNextQuesAndSave);
 saveAndNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),
    "Question added", Toast.LENGTH_SHORT).show();
 } });
 } @Override public void onClick(View v) { } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start,
    int before, int count) { } @Override public void afterTextChanged(Editable s) { } I want to use it within the following;
 public class Screen4 extends Screen2 implements View.OnClickListener { TextView testBumbleTitle;
 TextView testBumbleQues;
 TextView
    testEnterAns;
 TextView testQuesHint;
 Button yourNextQuesandSave;
 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
 setContentView(R.layout.screen4);
 testBumbleTitle = (TextView) findViewById(R.id.testBumbleTitle);

    testBumbleQues = (TextView) findViewById(R.id.testBumbleQues);
 testQuesHint = (TextView) findViewById(R.id.testQuesHint);
 testEnterAns = (TextView) findViewById(R.id.testEnterAns);
 yourNextQuesandSave = (Button) findViewById(R.id.yourNextQuesAndSave);

    testBumbleTitle.setText(yourQuizName);
 for (int i=0;i






2 个答案:

答案 0 :(得分:3)

你应该把你的数组作为额外的Intent

intent.putExtra("string-array", stringArray);
context.startActivity(intent);

和第二项活动:

String [] stringArray = intent.getStringArrayExtra("string-array");

答案 1 :(得分:0)

使用intent

在当前活动中:

Intent myIntent = new Intent(getApplicationContext(), classYouAreMovingTo.class);
myIntent.putExtra("myarray", myArray);
startActivity(myIntent);`

在您正在检索数据的活动中(在此示例中,它是classYouAreMovingTo)

Intent previousScreenIntent = getIntent();
String[] myArray =  previousScreenIntent.getStringArrayExtra("myarray");