简体   繁体   English

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

[英]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.

相关问题 Android Studio - 如何从 RecyclerView 中的 EditText 获取数据并将它们放入主要活动中的 ArrayList? - Android Studio - How to get data from an EditText in a RecyclerView and put them into an ArrayList in the main activity? 如何从可充气的Edit Text中读取值并将其放入ArrayList中 - How to read values from inflatable Edit Text and put them in an ArrayList 从 Firestore 中读取 arrays 并将它们放入 ArrayList - Java 中的 ZE84E30B9390CDB64DB6ZDB2CD9 - Read arrays from Firestore and put them inside an ArrayList - Java in Android Studio 如何从arraylist中检索多个值到jsp页面 - How to retrive multiple values to jsp page from arraylist 如何将Textviews放入数组并findViewById呢? - How to put Textviews in an array and findViewById them? 如何对ListView中的TextViews的值求和并相乘,并在烤面包中显示它们? - How to sum and multiply values from TextViews in ListView and display them in a toast? 从Firebase Realtime数据库中获取值,并将其放入ArrayList中 - Grab values from the Firebase Realtime database and put them in an ArrayList 我可以在Android Studio中创建一个包含TextView和ImageView的ArrayList吗? - Can I create an ArrayList that contains TextViews and ImageViews in Android Studio? 如何从不同的类中获取数组列表中的项目 - How to get an item in an arraylist from a diffrent class 如何将数据从不同的类添加到Arraylist - How to add data to a Arraylist from a diffrent class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM