ListView多条目加载

package mylistviewmore.mylistviewmore.NetUtils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

//网络工具类
public class NetUtils {
    public static String getNetData(String path) {
        // TODO Auto-generated method stub
        String result = "";
        //https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=1
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            int code = conn.getResponseCode();
            if(code == 200){
                InputStream inputStream = conn.getInputStream();
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                byte [] buff = new byte[1024];
                int len = 0;
                while((len = inputStream.read(buff)) != -1){
                    bos.write(buff, 0, len);
                }
                result = bos.toString();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;

    }

}
==================================================
package mylistviewmore.mylistviewmore;

import android.app.Application;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

//切记到image_loader jar包
//网络加载图片处理
 public class App extends Application {
    @Override
    public void onCreate() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisk(true)
                .cacheInMemory(true).build();

        ImageLoaderConfiguration configs = new ImageLoaderConfiguration.Builder(
                this).defaultDisplayImageOptions(options).threadPoolSize(5)
                .build();

        ImageLoader.getInstance().init(configs);


        super.onCreate();
    }
}
========================================
package mylistviewmore.mylistviewmore;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import mylistviewmore.mylistviewmore.NetUtils.NetUtils;
import mylistviewmore.mylistviewmore.NewDatas.Newdatas;

public class MainActivity extends AppCompatActivity {

    MyAdapter adapter;
    Newdatas newdatas;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        lv = (ListView) findViewById(R.id.lv);
        //设置数据
        initData();
        //设置适配器
        adapter = new MyAdapter();
        lv.setAdapter(adapter);
    }
    private void initData() {
        // TODO Auto-generated method stub
        new AsyncTask, String, String>() {
            @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub
                String json= NetUtils.getNetData("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=5");
               //切记导Gson jar包
               Gson gson = new Gson();
                newdatas = gson.fromJson(json , Newdatas.class);
                newdatas.getNewslist().addAll(newdatas.getNewslist());
                return  null;
            }
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                //更新适配器
                adapter.notifyDataSetChanged();
                Log.i("==========", newdatas.toString());
                super.onPostExecute(result);
            }
        }.execute();
    }
    class MyAdapter extends BaseAdapter {
        public static final int TYPE_ONE = 0;
        public static final int TYPE_TWO = 1;

        @Override
        public int getCount() {
            if (newdatas != null) {
                return newdatas.getNewslist().size();
            }
            return 0;
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public int getItemViewType(int position) {
//判断view类型
            int p = position % 2;
            if (p == 0) {
                return TYPE_ONE;
            } else {
                return TYPE_TWO;
            }
        }
        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            ViewHolder1 holder1 = null;
            int type = getItemViewType(position);
            if (convertView == null) {
            if (type==TYPE_ONE){
                        holder = new ViewHolder();
                        convertView = View.inflate(MainActivity.this, R.layout.list_item_one, null);
                        holder.lv_iv = (ImageView) convertView.findViewById(R.id.lv_iv);
                        holder.lv_tv = (TextView) convertView.findViewById(R.id.lv_tv);
                        holder.lv_time = (TextView) convertView.findViewById(R.id.lv_time);
                        convertView.setTag(holder);
            }else {
                holder1 = new ViewHolder1();
                convertView = View.inflate(MainActivity.this, R.layout.list_item_two, null);
                holder1.lv2_iv = (ImageView) convertView.findViewById(R.id.lv2_iv);
                convertView.setTag(holder1);
            }
            } else {
                if (type==TYPE_ONE){
                holder = (ViewHolder) convertView.getTag();
                }
                else {
               holder1 = (ViewHolder1) convertView.getTag();
                }
            }
            if (type==TYPE_ONE){
            ImageLoader.getInstance().displayImage(newdatas.getNewslist().get(position).getPicUrl(), holder.lv_iv);
            holder.lv_tv.setText(newdatas.getNewslist().get(position).getTitle());
            holder.lv_time.setText(newdatas.getNewslist().get(position).getCtime());}
            else {
                ImageLoader.getInstance().displayImage(newdatas.getNewslist().get(position).getUrl(), holder1.lv2_iv);
            }
            return convertView;
        }
    }
    class ViewHolder {
        ImageView lv_iv;
        TextView lv_tv;
        TextView lv_time;
    }
    class ViewHolder1{
        ImageView lv2_iv;
    }
}

 

 

你可能感兴趣的:(ListView多条目加载)