IT月光

导航

AsyncTask异步交互

package com.baidu.day06asynctask;

import java.util.ArrayList;
import java.util.List;

import com.baidu.adapter.MyAdapter;
import com.baidu.mynetutil.NewUtil;
import com.baidu.vo.MyData;
import com.baidu.vo.SuperFrist;
import com.google.gson.Gson;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

public class ShowActivity extends Activity{
    private ListView listView;

    @Override
    protected void onCreate(Bundle arg0) {
        // TODO Auto-generated method stub
        super.onCreate(arg0);
        setContentView(R.layout.activity_show);
        init();
    }

    private void init() {
        //找到相应的控件
        listView = (ListView) findViewById(R.id.lv);
        if(NewUtil.Netisavilable(ShowActivity.this)){
            
            new MyAsyncTask().execute(NewUtil.jSON_URL);
        }
        
    }
    class MyAsyncTask extends AsyncTask<String,Integer,String>{

        private List<MyData> list;
        @Override
        protected String doInBackground(String... arg0) {
            String str=arg0[0];
            String jstr=NewUtil.Check_Json(str);
            return jstr;
        }
        @Override
        protected void onPostExecute(String result) {
            //解析json
            Gson g=new Gson();
            SuperFrist sf=g.fromJson(result, SuperFrist.class);
            //创建一个集合容器
            list = sf.getResult().getData();
            //创建适配器
            listView.setAdapter(new MyAdapter(ShowActivity.this,list));
            super.onPostExecute(result);
        }
        
    }

}

 

posted on 2016-04-29 21:12  IT月光  阅读(152)  评论(0编辑  收藏  举报