Read a large text file into Textview

后端 未结 4 1489
逝去的感伤
逝去的感伤 2021-01-03 00:03

I want to read large file from sdcard into text view. I have idea but i don\'t know how to apply.

I think this things need to use: Handler And Thread

But i d

4条回答
  •  一个人的身影
    2021-01-03 00:49

    Your problem is that you are accessing a View owned by the GUI thread from the thread that is reading your file:

    subtitletv.setText(text.toString());
    

    You need to read the file, then pass its contents to the main thread to be displayed.

    //Create a handler on the UI Thread:
    private Handler mHandler = new Handler();
    
    //Executed in a non-GUI thread:
    public void updateView(){
        final String str = TheDataFromTheFile;
        mHandler.post(new Runnable(){
            @Override
            public void run() {
                 subtitletv.setText(str);
            }
        }
    }
    

提交回复
热议问题