咨询_异步类AsyncTask+http+listView

MainActivity

package com.zsd.frag;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.zsd.adapter.MyAdapter;
import com.zsd.app.R;
import com.zsd.bean.Json;
import com.zsd.masync.MyasynTask;

import java.util.ArrayList;

public class Frag_01 extends Fragment {
    public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    private ListView listView;
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
    private MyAdapter adapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag1, container, false);
        listView = view.findViewById(R.id.listView1);
        adapter = new MyAdapter(list, getActivity());
        listView.setAdapter(adapter);
        //传值
        new MyasynTask(list).execute(urlString);
        //刷新
        adapter.notifyDataSetChanged();
        return view;
    }
}

MyAdapter
package com.zsd.adapter;

import android.content.Context;
import android.media.Image;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zsd.app.R;
import com.zsd.bean.Json;
import com.zsd.masync.MyBitmapasyncTask;

import java.util.ArrayList;

public class MyAdapter extends BaseAdapter {
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();
    private Context context;

    public MyAdapter(ArrayList<Json.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        if (convertView==null){
            convertView = View.inflate(context, R.layout.item,null);
            holder.text1 = convertView.findViewById(R.id.text1);
            holder.image1 = convertView.findViewById(R.id.imageView1);
            convertView.setTag(holder);
        }else {
            holder = (Holder) convertView.getTag();
        }
        new MyBitmapasyncTask(holder.image1).execute(list.get(position).getPic_url());
        holder.text1.setText(list.get(position).getNews_summary());
        return convertView;
    }
    class Holder{
        TextView text1;
        ImageView image1;
    }
}
MyBitmapasyncTask 图片
package com.zsd.masync;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;

import com.zsd.net.NetWorkUtils;

public class MyBitmapasyncTask extends AsyncTask<String , View, Bitmap> {
    ImageView image1;

    public MyBitmapasyncTask(ImageView image1) {
        this.image1 = image1;
    }

    @Override
    protected Bitmap doInBackground(String... stirngs) {
        Bitmap bitmap = NetWorkUtils.getBitmapJson(stirngs[0]);
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        image1.setImageBitmap(bitmap);
    }
}
MyasynTask
package com.zsd.masync;


import android.os.AsyncTask;
import android.view.View;

import com.google.gson.Gson;
import com.zsd.bean.Json;
import com.zsd.net.NetWorkUtils;

import java.util.ArrayList;

public class MyasynTask extends AsyncTask<String ,View, String> {
    private ArrayList<Json.DataBean> list = new ArrayList<Json.DataBean>();

    public MyasynTask(ArrayList<Json.DataBean> list) {
        this.list = list;
    }
    //主线程
    @Override
    protected String doInBackground(String... strings) {
        String json = NetWorkUtils.getJson(strings[0]);
        return json;
    }
    //子线程
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        Json json = gson.fromJson(s, Json.class);
        ArrayList<Json.DataBean> listtemp = json.getData();
        list.addAll(listtemp);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值