[英]How to retrive values from ArrayList and put them in diffrent TextViews in Android Studio?
I have the following ArrayList: ArrayList<Integer> counters;
我有以下 ArrayList:
ArrayList<Integer> counters;
, with 3 values in it. ,其中有 3 个值。
I made an Intent to move this ArrayList from Main.class
, to a new class, callesd: two.class
.我做了一个意图将这个 ArrayList 从
Main.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
.我想从 ArrayList 中获取每个值,并将它们放入三个 TextViews
tv1, tv2, tv3
, tv1, tv2, tv3
,如下所示: tv1, tv2, tv3
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?那么,我需要做什么才能将 ArrayList 中的每个值放入三个不同的 TextView 中?
In your Main.class do this在您的 Main.class 中执行此操作
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在 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
让我知道它是否有效: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():将 TextView 代码移至 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 :
根据您在 pastebin 上发布的内容,您的 OnClick 和 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. NullPointerException 可能是因为列表为空。
Try this .尝试这个 。
initialize ArrayList<Integer> counters;
初始化
ArrayList<Integer> counters;
and ArrayList<Integer> seeAnsC;
和
ArrayList<Integer> seeAnsC;
change method to seeAns.putIntegerArrayListExtra("seeAnswers", counters);
将方法更改为
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.