我在AsyncTask的node.js
方法中解析天气数据并将解析后的数据存储在字符串中:
doInBackground()
我的UI中有2个TextView。如何使用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();
...
和onPostExecute()
字符串在stationId
中更新它们?
答案 0 :(得分:1)
将doInBackground
返回类型从Element
更改为Void
。现在在onPostExecute
中,您将获得Element
的对象。使用此对象提取值并设置为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 );
}
}
}