处理程序不延迟 - Android Studio Java

Handler does not delay - Android Studio Java

我正在做一个项目。我的程序显示 videoview 的视频并从 excel 文件中获取值。我很难一一显示这些值。我正在尝试使用一些延迟方法。我使用了 Thread.sleep 和 SystemClock.sleep 方法,但它们冻结了整个应用程序和 phone。现在我正在尝试使用处理程序方法。我没有收到任何错误,但没有延迟。我把我的代码放在下面。

如果有人能帮我解决这个延迟,我会很高兴。

package com.example.exceldeneme;

import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.MediaController;
import android.widget.VideoView;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.lang.String;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.List;


public class MainActivity<number_cars> extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        VideoView videoView = findViewById(R.id.video_view);
        Button  button = findViewById(R.id.button);
        TextView text_cars = findViewById(R.id.textEmpty);
        TextView text_total_lots = findViewById(R.id.textTotalLots);
        readData();
        int number_of_park = 100;
        text_total_lots.setText(String.valueOf(number_of_park));
        int counter = 0;

        String videoPath = "android.resource://" + getPackageName() + "/" + R.raw.camera_out;
        Uri uri = Uri.parse(videoPath);
        videoView.setVideoURI(uri);
        MediaController mediaController = new MediaController(this);
        videoView.setMediaController(mediaController);
        mediaController.setAnchorView(videoView);
        
        for (String i : number_of_cars.get(0)){
            counter = counter + 1 ;
        }

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int counter = 0;
                for (String i : number_of_cars.get(0)){
                    counter = counter + 1 ;
                }
                for (int i = 1; i < counter-1;i= i +1){
                    String count_park = number_of_cars.get(0)[i];
                    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            text_cars.setText(count_park);
                        }
                    }, 1000);


                    //SystemClock.sleep(1000); //ms
                    Log.d("Selam","SORUN YOK");
                }
            }
        });


    }


    private ArrayList<String[]> number_of_cars = new ArrayList<String[]>();
    private void readData() {
        InputStream is = getResources().openRawResource(R.raw.numberofcars);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is, Charset.forName("UTF-8"))
        );
        String line = "";
            try {
                while(((line = reader.readLine()) != null)) {
                String[] tokens = line.split(",");
                                number_of_cars.add(tokens);
                Log.d("MyActivity","Just created"+ Arrays.toString(tokens));
                }
            }
            catch (IOException e) {
                Log.wtf("MyActivity","Error reading data file on line" + line, e);
                e.printStackTrace();
            }
    }
}

对于您可能想要的孤立问题:

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        text_cars.setText(count_park);
                    }
                }, 1000 * i);

但是你真的应该有一个实例 Handler 和 post 所有 Runnable 对象,并有办法删除所有未决的 Runnable,如果有的话ui 在相关的钩子方法中使用 Handler::removeCallbacksAnMessages(null) 销毁,如 onDestroy.