Now i am doing an android application.In that application I am using asynchtask class to parse json data and i have to set that values in to ui.My program is like this.
class Activity
{
textview tv;
oncreate()
{
setcontentview(layout);
tv=(textview)findviewbyid(id);
call to pageload()
}
class PageLoad extends AsyncTask<String, Void, Boolean>
{
doInBackground()
{
json parsing
tv.setText(data);
}
}
}
But I couldn’t set the value from pageload class.Please help me friends.
You need to understand that doInBackground() runs on a different thread to the UI, therefore you cannot access the UI here. AsyncTask has two other methods:
onPreExecute()– beforedoInBackground()is runonPostExecute()– afterdoInBackground()is run, can receive the result of the work done by doInBackground()You need to pass the result of
doInBackground()toonPostExecute()where you can then set the TextView’s text.I would suggest you read Google’s article on how to use the Asynctask which can be found at http://developer.android.com/resources/articles/painless-threading.html and look at the class AsyncTask.
Example Code