如何从ArrayList中检索值并将它们放入Android Studio中的不同TextView中?
我有以下ArrayList:ArrayList<Integer> counters;
,其中有3个值。如何从ArrayList中检索值并将它们放入Android Studio中的不同TextView中?
我做了一个意图将这个ArrayList从Main.class
,移动到一个新的类,callesd:two.class
。 我想从ArrayList中获取每个值,并将它们放在三个TextView中,tv1, tv2, tv3
, 像这样:tv1 = value1, tv2 = value2, tv3 = value3
。 这里是我的代码:
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");
}
所以,我需要做的就是ArrayList中的每个值分成三个不同的TextViews?
试试这个。
初始化
ArrayList<Integer> counters;
和ArrayList<Integer> seeAnsC;
变化方法来
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);
}
两.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");
}
如果我理解正确,这应该是你要找的。让我知道,如果它的工作原理: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));
}
我不知道为什么,但它是崩溃 –
编辑你的答案与你在logcat上得到的错误如果我看不到错误,我不知道发生了什么 – Tharkius
pastebin: pastebin:https://pastebin.com/pS7VmWE4 –
你应该使用:
seeAns.putIntegerArrayListExtra("seeAnswers", counters);
代替:
seeAns.putExtra("seeAnswers", counters);
更新:
移动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));
}
仍然是crahsing。 –
是“two.class”的一个片段?如果你的视图没有在onCreate中创建( )。您必须更新“\t onViewCreated(View view,Bundle savedInstanceState)”中的TextViews。“ –
另外,在Activity中,视图在onStart()中可见。 –
请发布您的代码,以便我们可以为您提供帮助。根据你的贴在引擎收录,你有你的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)
的NullPointerException异常可能是因为该列表为空。
在你的主。类做到这一点
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);
而且在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));
你,当你问一个问题真的应该张贴所有(!)相关的代码。你在pastebin上发布的logcat信息(你应该在你的问题上发布在SO上)表明从你的'onClick'方法发起的错误 - 你没有发布!并且您有一个java.lang.NullPointerException:尝试调用空对象引用错误上的虚拟方法'boolean java.util.ArrayList.add(java.lang.Object)'。 – Barns
NullPointerException意味着你可能没有初始化你的“计数器”ArrayList“ – Barns
我发现我的错误。我试图得到未知的ArrayList,因为名称不匹配。我已经把(countCorrect>但得到得到(seeAnswers) –