for循环中的计数器变量仍然没有实时更改/计数为什么?

时间:2015-08-05 06:57:55

标签: java android android-studio

我需要一遍又一遍地点击按钮,在屏幕上看到计数更新。但我希望它能在FOR循环工作时实时更新。

在我的MainActivity顶部,我添加了:

private TextView text;
private RelativeLayout.LayoutParams params;
private int counter = 0;

然后在onCreate里面我添加了:

params =
                new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);


        ipaddresses = IpAddresses.GenerateIps();


        addListenerOnButton();

然后在按钮单击方法addListenerOnButton:

public void addListenerOnButton()
    {

        btnClick = (Button) findViewById(R.id.checkipbutton);

        btnClick.setOnClickListener(new OnClickListener()
        {
            byte[] response = null;
            @Override
            public void onClick(View arg0)
            {

                text = (TextView) findViewById(R.id.textView2);

                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        for (int i = 0; i < ipaddresses.length; i++)

                        {

                                try
                                {
                                    counter++;
                                    text.post(new Runnable()
                                    {
                                        @Override
                                        public void run()
                                        {

                                            text.setText("Checking Connection With Ip: " + ipaddresses[counter]);
                                            text.setLayoutParams(params);
                                        }
                                    });

                                    response = Get(ipaddresses[i]);
                                }
                                catch (Exception e)
                                {
                                    String err = e.toString();
                                }

                                if (response!=null)
                                {

                                    try
                                    {
                                        final String a = new String(response,"UTF-8");


                                        text.post(new Runnable()
                                        {
                                            @Override
                                            public void run()
                                            {
                                                text.setText(a);
                                            }
                                        });

                                        Logger.getLogger("MainActivity(inside thread)").info(a);
                                    } catch (UnsupportedEncodingException e)
                                    {
                                        e.printStackTrace();
                                        Logger.getLogger("MainActivity(inside thread)").info("encoding exception");
                                    }

                                    Logger.getLogger("MainActivity(inside thread)").info("test1");
                                    break;

                                }

                                else
                                {

                                }
                        }
                        if (response == null)
                        {
                            text.post(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    text.setText("Connection Failed");
                                }
                            });
                        }
                    }
                });
                t.start();
            }
        });

    }

但我面临两个问题:

  1. 当我在FOR循环内部进行循环时:

    text.setLayoutParams(PARAMS);

  2. 然后textView2跳跃将其位置更改为屏幕顶部。 但我希望它保持在相同的高度水平,只是向左移动,所以所有的文字都在这一行。

    第二个问题是现在我使用计数器作为计数变量。 但是当FOR循环工作时,计数器不是实时计数我需要一遍又一遍地点击按钮以在屏幕上看到变化。 我希望在不点击所有时间的情况下将其显示在屏幕上计数。

    text.setText("Checking Connection With Ip: " + ipaddresses[counter]);
    

    出于某种原因而不是实时自动在屏幕上显示我:

    检查与Ip的连接:10.0.0.2然后 检查与Ip的连接:10.0.0.3然后 检查与Ip的连接:10.0.0.4

    我需要点击按钮才能看到此更改。

    1. 为什么textView2不会向左移动而是向上移动?

    2. 如何使用计数器变量实时计数和显示?

    3. 如果我尝试使用for(int i = 0 如果我将使用I变量,它将给我错误,如果我将I更改为final,那么我不能在for中使用它(i = 0 ....因为你不能分配最终变量。

      所以我试图使用计数器,但它不会实时显示计数的进度。

1 个答案:

答案 0 :(得分:0)

不允许您更新线程中的视图。从您的计数器线程调用runOnUIThread()来更新文本视图。