listview加载图片

package com.example.day8;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {
    public static final String TAG="MainActivity";
    private ListView listView;
    private List<News.DataBean> list;
    private NewsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.listview);
        list = new ArrayList<>();
        adapter = new NewsAdapter(this, list);
        listView.setAdapter(adapter);
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String result=HttpUtils.getHttpFroment(strings[0]);
                Log.i(TAG, "doInBackground: 121321"+result);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                News news=gson.fromJson(s,News.class);
                if(news.getData()!=null ){
                    list.clear();
                    list.addAll(news.getData());
                    adapter.notifyDataSetChanged();
                }
            }
        }.execute("https://www.apiopen.top/satinApi?type=1&page=1");
    }
}

package com.example.day8;

import android.app.Application;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.io.IOException;

/**
 * Created by 。。。。 on 2018/9/7.
 */

public class BaseApeltion extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        File file = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File rootSD = Environment.getExternalStorageDirectory();
            file = new File(rootSD, "image");
            if (!file.exists()) {
                file.mkdirs();
            }
        }
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
        try {
            DiskCache diskCache = new LruDiskCache(file, new Md5FileNameGenerator(), 50 * 1024 * 1024);
            ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)
                    .diskCache(diskCache)
                    .defaultDisplayImageOptions(options)
                    .threadPoolSize(3)
                    .build();

            ImageLoader.getInstance().init(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

package com.example.day8;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.cache.disc.DiskCache;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by 。。。。 on 2018/9/7.
 */

public class NewsAdapter extends BaseAdapter {
    private Context context;
    private List<News.DataBean> list;

    public NewsAdapter(Context context, List<News.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @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) {
        ViewHolder holder=null;
        if(convertView==null){
            convertView=View.inflate(context,R.layout.item_news,null);
            holder=new ViewHolder();
            holder.img=convertView.findViewById(R.id.image);
            holder.text=convertView.findViewById(R.id.text);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(list.get(position).getCdn_img(),holder.img);
        holder.text.setText(list.get(position).getText());
        return convertView;
    }
    class ViewHolder{
        ImageView img;
        TextView text;
    }
}
展开阅读全文

没有更多推荐了,返回首页