I have the following ArrayList: ArrayList<Integer> counters;
, with 3 values in it.
I made an Intent to move this ArrayList from Main.class
, to a new class, callesd: two.class
. And I want to get each value from the ArrayList, and put them in three TextViews, tv1, tv2, tv3
, like this: tv1 = value1, tv2 = value2, tv3 = value3
. Here is my code:
Main.class:
public void moveToAns(View view) {
counters.add(countCorrect);
counters.add(countWrong);
counters.add(countTotal);
seeAns = new Intent(this, two.class);
seeAns.putExtra("seeAnswers", counters);
startActivity(seeAns);
}
two.class:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
intentSee = getIntent();
seeAnsC = intentSee.getIntegerArrayListExtra("seeAnswers");
}
so, what I need to do to get each value from the ArrayList into three different TextViews?
In your Main.class do this
public void moveToAns(View view) {
ArrayList<Integer> counters = new ArrayList();// you can declare it globally
counters.add(countCorrect);
counters.add(countWrong);
counters.add(countTotal);
seeAns = new Intent(this, two.class);
seeAns.putIntegerArrayListExtra("seeAnswers", counters);
startActivity(seeAns);
And in Two.class
ArrayList<Integer> counters = getIntent().getIntegerArrayListExtra("seeAnswers");
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv1.setText("Correct answers: " + seeAnsC.get(0));
tv2.setText("Wrong answers: " + seeAnsC.get(1));
tv3.setText("Total answers: " + seeAnsC.get(2));
If I understand correctly, this should be what you're looking for. Let me know if it works :D
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
intentSee = getIntent();
seeAnsC = intentSee.getIntegerArrayListExtra("seeAnswers");
tv1.setText("Correct answers: " + seeAnsC.get(0));
tv2.setText("Wrong answers: " + seeAnsC.get(1));
tv3.setText("Total answers: " + seeAnsC.get(2));
}
you should use:
seeAns.putIntegerArrayListExtra("seeAnswers", counters);
instead of:
seeAns.putExtra("seeAnswers", counters);
Update:
Move the TextView code to onStart():
private ArrayList<Integer> seeAnsC;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two)
intentSee = getIntent();
seeAnsC = intentSee.getIntegerArrayListExtra("seeAnswers");
}
protected void onStart() {
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
tv1.setText("Correct answers: " + seeAnsC.get(0));
tv2.setText("Wrong answers: " + seeAnsC.get(1));
tv3.setText("Total answers: " + seeAnsC.get(2));
}
Please post your code, so we can help you. According to what your posted on pastebin, you have an error in your OnClick and a NullPointerException :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.user.thordrandom.Main.moveToAns(Main.java:108)
The NullPointerException is probably because the list is null.
Try this .
initialize ArrayList<Integer> counters;
and ArrayList<Integer> seeAnsC;
change method to seeAns.putIntegerArrayListExtra("seeAnswers", counters);
Main.class
ArrayList<Integer> counters;
public void moveToAns(View view) {
// Initialization
counters = new ArrayList<>();
counters.add(countCorrect);
counters.add(countWrong);
counters.add(countTotal);
seeAns = new Intent(this, two.class);
// change method to putIntegerArrayListExtra
seeAns.putIntegerArrayListExtra("seeAnswers", counters);
startActivity(seeAns);
}
Two.class
ArrayList<Integer> seeAnsC;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
tv3 = (TextView) findViewById(R.id.tv3);
intentSee = getIntent();
// init
seeAnsC = new ArrayList<>();
seeAnsC = intentSee.getIntegerArrayListExtra("seeAnswers");
}
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.