android 回调从AsyncTask往Activity传数据

1.在LoadTask

( 1

//设置回调借口
public interface callBack{
    void setItem(Item i);
}

( 2
//给LoadTask添加设置回调的方法
    public void setCallBack(callBack callback){
        this.callBack = callback;
    }

( 3
//LoadTask的参数callBack
    private callBack callBack;

( 4 在onPostExecute方法里
if (callBack != null){
                callBack.setItem(item);
            }

( 5 完整的LoadTask
package app.coolwhether.com.weatherapp.Http;

import android.os.AsyncTask;
import android.util.Log;

import app.coolwhether.com.weatherapp.activity.WeatherShow;
import app.coolwhether.com.weatherapp.entity.Item;
import app.coolwhether.com.weatherapp.support.JsonHelper;

/**
 * Created by kirito on 2016/9/4.
 */
public class LoadTask extends AsyncTask<String,Void,String> {
    private Item item;
    //LoadTask的参数callBack
    private callBack callBack;
    private static final String TAG = "LoadTask";

    @Override
    protected String doInBackground(String... params) {
        return Http.getUrlData(params[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        //Log.e(TAG, "onPostExecute: s---"+s);
        try{
            item = JsonHelper.parseJsonToItem(s);
            //如果callBack不为空,把item参数传给WeatherShow.activity
            if (callBack != null){
                callBack.setItem(item);
            }
            Log.e(TAG, "onPostExecute: item---"+item );
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //给LoadTask添加设置回调的方法
    public void setCallBack(callBack callback){
        this.callBack = callback;
    }

    //设置回调借口
    public interface callBack{
        void setItem(Item i);
    }

}

2.在需要获取数据的Activity里:
 
 
package app.coolwhether.com.weatherapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import app.coolwhether.com.weatherapp.Http.Http;
import app.coolwhether.com.weatherapp.Http.LoadTask;
import app.coolwhether.com.weatherapp.R;
import app.coolwhether.com.weatherapp.entity.Item;
import app.coolwhether.com.weatherapp.support.JsonHelper;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by kirito on 2016/9/2.
 */
public class WeatherShow extends AppCompatActivity {
    private String id;
    private static final String path_left = "https://api.heweather.com/x3/weather?cityid=";
    private static final String path_right = "&key=9bb0507d23b046cb9b6fee3c6053f985";
    private String url;
    private Item item;

    @BindView(R.id.time_tv_show)
    TextView date_tv;
    @BindView(R.id.wind_sc)
    TextView sc_tv;
    @BindView(R.id.wind_dir)
    TextView dir_tv;
    @BindView(R.id.ss_show)
    TextView ss_tv;
    @BindView(R.id.sr_show)
    TextView sr_tv;
    @BindView(R.id.temp_min_show)
    TextView temp_min_tv;
    @BindView(R.id.temp_max_show)
    TextView temp_max_tv;
    @BindView(R.id.txt_d_show)
    TextView txt_d_tv;
    @BindView(R.id.txt_n_show)
    TextView txt_n_tv;

    private static final String TAG = "WeatherShow";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather);
        ButterKnife.bind(this);

        setTitle(getIntent().getStringExtra("name"));
        id = getIntent().getStringExtra("id");
        url = path_left + id + path_right;

        //new一个LoadTask,设置回调接口,在获取到item后执行与item有关的操作
        //不在获取到item后就使用item会报空指针
        LoadTask task = new LoadTask();
        task.setCallBack(new LoadTask.callBack() {
            @Override
            public void setItem(Item i) {
                item = i;
                date_tv.setText(item.getDate());
                sc_tv.setText(item.getWind_sc());
                dir_tv.setText(item.getWind_dir());
                sr_tv.setText(item.getSr());
                ss_tv.setText(item.getSs());
                temp_min_tv.setText(item.getTmp_min());
                temp_max_tv.setText(item.getTmp_max());
                txt_d_tv.setText(item.getTxt_d());
                txt_n_tv.setText(item.getTxt_n());
            }
        });
        //执行task
        task.execute(url);
        //new LoadTask().execute(url);
    }

   
}

 
 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值