问题
I'm parsing weather data in AsyncTask's doInBackground()
method and storing parsed data in strings:
protected Void doInBackground(Void... params) {
...
Element element = (Element) list.item(i);
if (element.getNodeName().equals("station_id")){
String stationId = element.getTextContent();
} else if (element.getNodeName().equals("observation_time") ){
String observationTime = element.getTextContent();
...
I have 2 TextViews in my UI. How do I update them in onPostExecute()
with stationId
and observationTime
strings?
回答1:
Change your doInBackground
return type to Element
from Void
. Now in onPostExecute
you will get object of Element
. Use this object to extract values and set into TextViews.
public class YourTask extends AsyncTask<Void, Void, Element> {
TextView textView1, textView2;
public YourTask (TextView textView1, TextView textView2) {
this.textView1 = textView1;
this.textView2 = textView2;
}
@Override
protected Element doInBackground(Void... params) {
...
Element element = (Element) list.item(i);
return element;
}
@Override
protected void onPostExecute(Element element) {
if (element.getNodeName().equals("station_id")){
String stationId = element.getTextContent();
textView1.setText(stationId);
} else if (element.getNodeName().equals("observation_time") ){
String observationTime = element.getTextContent();
textView2.setText(observationTime );
}
}
}
来源:https://stackoverflow.com/questions/35193069/update-textview-fields-in-onpostexecute