Pass a Message From Thread to Update UI

Posted by Jay Dee on Stack Overflow See other posts from Stack Overflow or by Jay Dee
Published on 2011-01-16T11:51:20Z Indexed on 2011/01/16 11:53 UTC
Read the original article Hit count: 237

Filed under:
|
|

Ive created a new thread for a file browser. The thread reads the contents of a directory. What I want to do is update the UI thread to draw a graphical representation of the files and folders. I know I can't update the UI from within a new thread so what I want to do is:

whilst the file scanning thread iterates through a directories files and folders pass a file path string back to the UI thread. The handler in the UI thread then draws the graphical representation of the file passed back.

public class New_Project extends Activity implements Runnable {

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {




            Log.d("New Thread","Proccess Complete.");
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();



    }
};


public void getFiles(){

    //if (!XMLEFunctions.canReadExternal(this)) return;

    pd = ProgressDialog.show(this, "Reading Directory.", "Please Wait...", true,
            false);

    Log.d("New Thread","Called");
Thread thread = new Thread(this);
thread.start();

}

public void run() {

    Log.d("New Thread","Reading Files");
    getFiles();
    handler.sendEmptyMessage(0);

}

public void getFiles() {

  for (int i=0;i<=allFiles.length-1;i++){

            //I WANT TO PASS THE FILE PATH BACK TU A HANDLER IN THE UI
            //SO IT CAN BE DRAWN.
    **passFilePathBackToBeDrawn(allFiles[i].toString());**

 } 


}

}

© Stack Overflow or respective owner

Related posts about android

Related posts about multithreading