更新onPostExecute()中的TextView字段

时间:2016-02-04 05:20:25

标签: android android-asynctask

我在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中更新它们?

1 个答案:

答案 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 );
      } 
   }
}