-2

この void を使用して、ログのようなプロセスで TextView を追加し、ユーザーに表示します。

static void addlog(Activity innercont, String txt)
    {
        TextView tadd = new TextView(innercont);
        tadd.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        tadd.setText(txt);
        Log.i(TAG,"addlog: "+txt);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(tadd);

        ScrollView scro = (ScrollView) innercont.findViewById(R.id.ScrollView1);
        scro.removeAllViews();
        scro.addView(layout);

        innercont.setContentView(scro);
    }

ほとんどのことが役に立たないことはわかっていますが、これが現時点での私の試みです。

問題

最初に (MainActivity-onCreate)、この void を使用して初期化エントリを追加します - 動作します。その後、この関数を使用して多くのエントリを持つ別の関数 (カスタム void) を呼び出す関数 (プライベート クラス navigata extends WebViewClient) があります。関数が終了するまでそれらのどれも表示されず (そしてそれには多くの時間がかかります)、ログ全体が役に立たなくなります (すべてが終了した後にしか見ることができないログは必要ありません)。

だから私の質問は次のとおりです。テキストビューを追加できるように、関数を一時停止するようにするにはどうすればよいですか?

4

1 に答える 1

0

Michael Butscher のおかげで、Threads を使用して問題を解決しました。

したがって、元の addlog-function を次のように変更しました。

static void addlog(Activity innercont, String txt)
    {
        if (innercont.findViewById(255) != null)
        {
            TextView tadd = (TextView) innercont.findViewById(255);
            String atmtxt = (String) tadd.getText();
            atmtxt = atmtxt+"\n"+txt;
            tadd.setText(atmtxt);
            return;
        }

        TextView tadd = new TextView(innercont);
        tadd.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        tadd.setText(txt);
        tadd.setId(255);

        layout.setOrientation(LinearLayout.VERTICAL);
        layout.addView(tadd);

        ScrollView scro = (ScrollView) innercont.findViewById(R.id.ScrollView1);
        scro.removeAllViews();
        scro.addView(layout);

        innercont.setContentView(scro);
    }

View.postまた、UI をブロックせずにアクセスするために使用する独自の addlog-function を持つスレッドで主な作業を行っていました。

final TextView tadd = (TextView) cont.findViewById(255);
        tadd.setText("");
        //Log.i(TAG, "get lists" );

        new Thread(new Runnable() {
            public void addlog2(final Activity cont, final String txt)
            {
                tadd.post(new Runnable() {
                    public void run() {
                        String atmtxt = (String) tadd.getText();
                        atmtxt = atmtxt+"\n"+txt;
                        tadd.setText(atmtxt);
                    }
                });
            }


            public void run() {
                addlog2(cont,"log text");
                                //...
              }
      }).start();
于 2013-07-05T11:05:36.783 に答える