Android 中的进度条和 TextView 更新

Progressbar and TextView updates in Android


我为我的问题创建了一个 "small" 示例。我有两个 classes:MainActivityBerechnung。 Class Berechnung 计算了一些应该花费一点时间的东西,仅用于测试进度条。
首先,MainActivity:

package com.example.matthias.progressbar;


import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class MainActivity extends AppCompatActivity implements Runnable{


    Button btn;
    ProgressBar progress;
    TextView tv;

    Berechnung langeBerechnung;
    ArrayList<Integer> erg;
    boolean laeuftNoch;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.button);
        progress = (ProgressBar) findViewById(R.id.progressBar);
        tv = (TextView) findViewById(R.id.textView);
    }



    @Override
    public void run() {
        int progressWert = 0;
        tv.setText("Berechnung gestartet.");
        while(laeuftNoch){
            progressWert = progressWert + 5;

            if(progressWert < 100){
                progress.setProgress(progressWert);
            }else {
                progressWert = 0;
                progress.setProgress(progressWert);
            }
        }
        tv.setText("Berechnung beendet.");
        progress.setProgress(100);
    }

    public void onClick(View v){
        handler.post(this);

        Callable<ArrayList<Integer>> call = new Berechnung();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Future<ArrayList<Integer>> future = executor.submit(call);

        try {
            ArrayList<Integer> erg = future.get();
            int index = ((Berechnung)call).getIndex();
            int grenze = ((Berechnung)call).getGrenze();
            Log.d("App", "Die längste Folge bis " + grenze + " entsteht für die Eingabe " + index + " und hat eine Länge von " + erg.size() + ".");
            ((Berechnung)call).ausgabe(erg);
        } catch (Exception e) {
            // Nothing
        }
        laeuftNoch = false;
    }


}

现在第二个Class:

package com.example.matthias.progressbar;

import android.provider.Settings;
import android.telecom.Call;
import android.util.Log;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class Berechnung implements Callable<ArrayList<Integer>> {

    ArrayList<Integer> list = new ArrayList<Integer>();
    ArrayList<ArrayList<Integer>> all = new ArrayList<ArrayList<Integer>>();
    int index;
    int grenze;

    public ArrayList<Integer> call(){
        all = new ArrayList<ArrayList<Integer>>();
        all.add(0, new ArrayList<Integer>());

        grenze = 35000;

        for(int i=1; i <= grenze; i++){
            Log.d("App", "Berechne collatz(" + i + ")");

            list = new ArrayList<Integer>();
            all.add(i, collatz(i));
        }

        int max = all.get(1).size();
        index = 1;
        for(int i=2; i <= grenze; i++){
            if(all.get(i).size() > max){
                max = all.get(i).size();
                index = i;
            }
        }


        return all.get(index);
    }


    public ArrayList<Integer> collatz(int n){
        list.add(n);
        if(n > 1) {
            if (n % 2 == 0) {
                n = n/2;
                return collatz(n);
            } else {
                n = 3*n + 1;
                return collatz(n);
            }
        }else{
            return list;
        }
    }

    public int getIndex(){
        return index;
    }

    public int getGrenze(){
        return grenze;
    }

    public void ausgabe(ArrayList<Integer> l){
        for(int i=0; i < l.size(); i++){
            Log.d("App", (i+1) + ".)\t" + l.get(i) + "");
        }
    }

}

我的问题可能出在MainActivity。 TextView (tv) 和 ProgressBar (progress) 都不会更新。在第一步中,我使用了一个普通的线程来更改文本视图和进度条,但是我得到了一个 来自错误线程异常的调用 。然后我在一些网页上读到,我需要一个处理程序来解决这个问题。但是我不明白我必须如何使用它。

也许 xml 文件有助于解决这个问题:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.matthias.progressbar.MainActivity">

    <ProgressBar
        android:layout_above="@+id/button"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="65dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="180dp"
        android:text="Starte Berechnung"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClick" />

    <TextView
        android:text='Drücken Sie den Button "Starte Berechnung".'
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="45dp"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="16sp" />
</RelativeLayout>

我不确定是哪个 class 造成了问题,因此我给了你所有的文件。 Berechnung-class 还使用控制台输出,它阻塞了线程。也许这也是一个问题(?)

拜托,任何人都可以帮助我理解我的编程错误吗?

此致,马蒂亚斯

对此我会采取完全不同的方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button) findViewById(R.id.button);
    progress = (ProgressBar) findViewById(R.id.progressBar);
    tv = (TextView) findViewById(R.id.textView);
}

在这个 onCreate 方法中,我会为按钮放置 onClick 侦听器。 (btn.setOnClickListener(new OnClickListener)) 在这种方法中,您可以编写 "run" 内容。

如果这不能真正回答您的问题,我可能误解了您的意思。 那么这些东西有什么作用呢?

@Override
public void run() {
    int progressWert = 0;
    tv.setText("Berechnung gestartet.");
    while(laeuftNoch){
        progressWert = progressWert + 5;

        if(progressWert < 100){
            progress.setProgress(progressWert);
        }else {
            progressWert = 0;
            progress.setProgress(progressWert);
        }
    }
    tv.setText("Berechnung beendet.");
    progress.setProgress(100);
}

public void onClick(View v){
    handler.post(this);

    Callable<ArrayList<Integer>> call = new Berechnung();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<ArrayList<Integer>> future = executor.submit(call);

    try {
        ArrayList<Integer> erg = future.get();
        int index = ((Berechnung)call).getIndex();
        int grenze = ((Berechnung)call).getGrenze();
        Log.d("App", "Die längste Folge bis " + grenze + " entsteht für die Eingabe " + index + " und hat eine Länge von " + erg.size() + ".");
        ((Berechnung)call).ausgabe(erg);
    } catch (Exception e) {
        // Nothing
    }
    laeuftNoch = false;
}

无论如何你都没有谈论它们,我们需要你的哪一部分 "Berechnung" Class?另外,您遇到的确切问题是什么?

编辑:

所以它似乎没有更新给你,因为你的线程中没有 sleep

while(laeuftNoch) {
    progressWert = progressWert + 5;

    if(progressWert < 100){
        progress.setProgress(progressWert);
    } else {
        progressWert = 0;
        progress.setProgress(progressWert);
    }
}

while-loop 不花时间!它并没有真正暂停或者我错过了什么?它在 1 毫秒内直接运行。所以你应该添加类似 Thread.sleep(milliseconds) 的内容。 还有在for loop中每次分配列表的目的是什么?

for(int i=1; i <= grenze; i++){
    Log.d("App", "Berechne collatz(" + i + ")");

    list = new ArrayList<Integer>();
    all.add(i, collatz(i));
}