本文介绍了更新onPostExecute()中的TextView字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用AsyncTask的doInBackground()
方法解析天气数据,并将解析后的数据存储在字符串中:
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();
...
我的UI中有2个TextViews.如何在onPostExecute()
中使用stationId
和observationTime
字符串更新它们?
I have 2 TextViews in my UI. How do I update them in onPostExecute()
with stationId
and observationTime
strings?
推荐答案
将您的doInBackground
返回类型从Void
更改为Element
.现在在onPostExecute
中,您将获得Element
的对象.使用此对象可以提取值并将其设置为TextViews.
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 );
}
}
}
这篇关于更新onPostExecute()中的TextView字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!