【问题标题】:Display values in ArrayList one by one in textView在textView中一一显示ArrayList中的值
【发布时间】:2019-07-20 04:09:49
【问题描述】:

我试图在一段时间后在单行 textView 上一一显示 ArrayList 内的值。如何在不阻塞主线程的情况下实现这一点?

我已经编写了能够使用 Thread.sleep 执行此操作的代码,但是在运行几秒钟后,活动崩溃了。我使用 For Loop & Thread.sleep 在一段时间后迭代每个 ArrayList 值。

当活动崩溃时,我在运行几秒钟后收到IndexOutOfBondException

public void errorRepeater() {

    Thread t = new Thread() {

        @Override
        public void run() {
            //  !isInterrupted()

            while (!isInterrupted()) {
                for (xz = 0; xz < errorList.size(); xz++) {
                    try {
                        Thread.sleep(2000);  //1000ms = 1 sec

                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                String sErrorList = errorList.get(xz);
                                String sErrorListOkBox = errorListOkBox.get(xz);
                                Log.i("MQTT sErrorList", sErrorList);
                                TextView tvC1HPLP = findViewById(R.id.errormsg);
                                tvC1HPLP.setText(sErrorList);
                                TextView tvok = findViewById(R.id.ok);
                                tvok.setText(sErrorListOkBox);
                                rl.setBackgroundResource(R.drawable.errorred);
                                tvC1HPLP.setTextColor(Color.RED);

                            }
                        });

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    };

    t.start();

}

textView 应该在 ArrayList 中一一显示值,而不会导致活动崩溃。

【问题讨论】:

  • 什么是errorListOkBox?,如果是两个list,大小不一样
  • errorListOkBox 也是 ArrayList。两个 ArrayList 的大小相同。虽然运行几秒钟后活动崩溃
  • 大小不同,这就是问题

标签: java android


【解决方案1】:

仅供参考,你可以试试这样的。

   // You can define those both textview globally.
   TextView tvC1HPLP = findViewById(R.id.errormsg);
   TextView tvok = findViewById(R.id.ok);

   Handler mHandler = new Handler();
   final Runnable runnable = new Runnable() {
     int count = 0;
     @Override
     public void run() {

         String sErrorList = errorList.get(count%errorList.size);
         String sErrorListOkBox = errorListOkBox.get(count%errorListOkBox.size);

         tvC1HPLP.setText(sErrorList);

         tvok.setText(sErrorListOkBox);
         rl.setBackgroundResource(R.drawable.errorred);
         tvC1HPLP.setTextColor(Color.RED);
         count++;
         mHandler.postDelayed(this, 4000); // four second in ms
     }
   };
   mHandler.postDelayed(runnable, 1000);

【讨论】:

  • 这对我来说似乎工作正常。当手机从睡眠模式进入唤醒模式时,它会导致活动崩溃。它在“String sErrorList = errorList.get(count%errorList.size());”行上说“java.lang.ArithmeticException:除以零”我应该在执行之前检查非零列表大小吗?
  • 添加了零大小检查。现在它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 2016-08-25
相关资源
最近更新 更多