I have a main activity where I create an adapter for a arraylist data.
I read the news from a website using a separate thread with Jsoup.
In onCreate() i have
this.newsItemAdapter = new NewsItemAdapter(this,
R.layout.newsitem_row,NewsItemAdapter.getAllNews());
parseThread.start();
this.newsItemAdapter.notifyDataSetChanged();
When i read from the adapter, i get empty list. This is because the thread is not completed yet. Any idea as to how i should proceed?
I cannot do notifyDataSetChanged inside my thread because it is not the owner of the adapter.
You need to notify after the seperate thread finishes, and invoke notifyDataSet on UI-thread, one possible method is to use handler,
you can define a handler in activity, invoke notifyDataSet change in handleMessage, such as
and in the thread run method, you need to send a message to the handler,
or else you can use AsyncTask instead of seperate thread, use jsoup in task’s doInBackground method, and notifyDataSet in onPostExecute.
The handler document is http://developer.android.com/reference/android/os/Handler.html, and AsyncTask’s is http://developer.android.com/reference/android/os/AsyncTask.html.