Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Painful threading in android

Tags:

android

My expected result is in the first launch there will display a progress dialog to wait for the background thread loading the content. The dialog dismisses after worker thread is done the job. I did search and got this solution How to display progress dialog before starting an activity in Android?

This is my complete code:

    private ManagerApplication app;
    private ImageAdapter adapter;
    private GridView gridview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupView();      
    }

    private void setupView() {
        gridview = (GridView) findViewById(R.id.gridview);
        app = (ManagerApplication) getApplication();
        ProgressDialog progress = new ProgressDialog(this);
        progress.setMessage("Loading...");
        new LoadImageTask(progress, this).execute();       
    }

    private class LoadImageTask extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progress;
        private Context context;

        public LoadImageTask(ProgressDialog progress, Context context) {
            this.progress = progress;
            this.context = context;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            adapter = new ImageAdapter(app.getShoes(), context);
            gridview.setAdapter(adapter); 
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progress.dismiss();
        }

    }

However, my application crashes with the reason that "Only the original thread that created a view hierarchy can touch its views". I suppose there are something blocked the main UI thread, but its still very unclear. So could anyone point me to the problem ? Thanks

like image 874
Harry Avatar asked Mar 28 '26 10:03

Harry


2 Answers

Funnily enough there's an article entitled Painless Threading that you should read. You shouldn't try to manipulate views in doInBackground. In your case, you can call app.getShoes() but don't do the adapter setup in there.

like image 187
kabuko Avatar answered Mar 31 '26 06:03

kabuko


doinBackground is non UI Thread ,So never update any UI(View) in this method...

and use OnPostExecute or OnProgressUpdate for update UI

 @Override
        protected Void doInBackground(Void... params) {
           //here just background task ,
     //its non UI Thread so dont set ant view here set it in OnPostExecute,...
            return null;
        }

call asynctask from setupview

private void setupView() {
        gridview = (GridView) findViewById(R.id.gridview);
        app = (ManagerApplication) getApplication();

        new LoadImageTask(progress, this).execute();       
  }

and create ProgressDilog in OnPreExecute method

ProgressDialog progress;

@Override
    protected void onPreExecute() {
        super.onPreExecute();

     progress = new ProgressDialog(this);
     progress.setMessage("Loading...");
     progress.show();
    }

and in onPostExecute dismiss it

like image 41
Samir Mangroliya Avatar answered Mar 31 '26 04:03

Samir Mangroliya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!