如何从ArrayList中检索值并将它们放在Android Studio中的不同TextView中?

时间:2017-10-13 21:31:34

标签: java android arraylist textview

我有以下ArrayList:ArrayList<Integer> counters;,其中包含3个值。

我做了一个Intent,将这个ArrayList从Main.class移动到一个新类callesd:two.class。 我想从ArrayList中获取每个值,并将它们放在三个TextViews 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中的每个值分成三个不同的TextView?

5 个答案:

答案 0 :(得分:1)

在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);

在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));

答案 1 :(得分:0)

如果我理解正确,这应该是您正在寻找的。让我知道它是否有效: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));
}

答案 2 :(得分:0)

你应该使用:

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));
}

答案 3 :(得分:0)

请发布您的代码,以便我们为您提供帮助。根据你在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)

NullPointerException可能是因为列表为空。

答案 4 :(得分:0)

试试这个。

  • 初始化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);
}

<强> 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");
}