AsyncTask 网络获取图片和Json数据加载到ListView上

package com.example.myzhoukaolianxi;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
//工具类
//
NetWorkUtil()
 
  
 
  
 
  
public class MainActivity extends AppCompatActivity {

    List lists=new ArrayList<>();
    private ListView lv;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new MyAdapter();
        initData();
    }
    //请求网络数据
    private void initData(){
        new MAsyncTaskString().execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
    }
    //获取字符串
    class MAsyncTaskString extends AsyncTask{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson=new Gson();
            Person person = gson.fromJson(s, Person.class);
            lists=person.getData();
            lv.setAdapter(adapter);
        }

        @Override
        protected String doInBackground(String... params) {
            return new NetWorkUtil().getNetString(params[0]);
        }


    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return lists.size();
        }

        @Override
        public Object getItem(int position) {
            return lists.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view=View.inflate(MainActivity.this,R.layout.item,null);
            TextView tv= (TextView) view.findViewById(R.id.tv);
            ImageView image= (ImageView) view.findViewById(R.id.imageView);

            tv.setText(lists.get(position).getNews_title());
            new MAsyncTaskImage(image).execute(lists.get(position).getPic_url());
            //把图片url传递给asyncTask



            return view;
        }
    }
    //获取图片
    class MAsyncTaskImage extends AsyncTask{
       ImageView image;

        public MAsyncTaskImage(ImageView image) {
            this.image = image;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            image.setImageBitmap(bitmap);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            return new NetWorkUtil().getNetImage(params[0]);
        }
    }

}

你可能感兴趣的:(AsyncTask 网络获取图片和Json数据加载到ListView上)