如何在主活动的for循环内的线程内生成TextView

时间:2018-07-21 22:06:17

标签: java android

是否可以在main活动的for循环内生成TextView?我必须运行另一个Java类“ multithread”中的线程。当此类运行时,我将知道连接了多少个客户端,我将运行内部线程来根据客户端的数量生成TextView,并在这些TextView中显示接收到的消息。

但是我遇到一个错误。如果您知道在主活动中运行线程的更好方法,请告诉我,谢谢。

这是方法:

public void toggleButtonConnectToClientsFunction(View view) {
Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    //Multithread here
                    multicastthreadRun.run();
                    for(int counter=0;counter<multicastthreadRun.ClientIpArrayList.size();counter++) {

                        TextView textView=new TextView(this);//i am obtaining error here
                        linearLayoutSecondaryTexts.addView(textView);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //in this case we can change the user interface



                        }
                    });
                    }//end of the for loop
                }
            });t.start();

3 个答案:

答案 0 :(得分:2)

new Runnable是一个匿名类,而this指向该匿名类。要创建文本视图,您需要传递context(活动或上下文),因此,请使用className作为类的特定引用来

TextView textView=new TextView(YourContainerActivityClassName.this);
// e.g     TextView textView=new TextView(MainActivity.this);

注意:由于它是点击侦听器,因此好像直接在活动中

并且您无法通过工作线程更新用户界面,因此就像

public void toggleButtonConnectToClientsFunction(View view) {
    Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                //Multithread here
                multicastthreadRun.run();
                for(int counter=0;counter<multicastthreadRun.ClientIpArrayList.size();counter++) {

                    // use proper context
                    TextView textView=new TextView(YourActivityNamethis);//i am obtaining error here

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // Update UI
                        linearLayoutSecondaryTexts.addView(textView);
                    }
                });
                }//end of the for loop
            }
        });t.start();

}

答案 1 :(得分:2)

  

TextView textView = new TextView(this); //我在这里获取错误

TextView textView = new TextView(getBaseContext());// textview expect context object NOT runnable object.

除了使用循环外,您还可以使用递归。

public void toggleButtonConnectToClientsFunction(View view) {
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            //Multithread here
            multicastthreadRun.run();
            makeTextView ( 0,multicastthreadRun.ClientIpArrayList.size());
        }
    });
    t.start();
}

private void makeTextView(final int count, final int max){
    if (count>= max)
        return; // end of Loop

    TextView textView = new TextView(getBaseContext());// Use can you this here as it will refer to your activity object.

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //in this case we can change the user interface
         linearLayoutSecondaryTexts.addView(textView);
         makeTextView(count+1, max);
        }
    });
}

答案 2 :(得分:1)

在您的代码TextView(this)中,由于它位于Thread中,因此将无法正常工作。

您必须准确指定上下文。检查以下代码:

TextView tv = new TextView(YOUR_ACTIVITY.this);
tv.setText("What to do");

谢谢:)