listview多條目展示


//listview多條目適配器


import android.content.Context;

import android.text.TextUtils;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


import com.bawei.Yuekao_exam_three.R;

import com.bawei.Yuekao_exam_three.bean.DataInfo;

import com.bawei.Yuekao_exam_three.bean.User;

import com.nostra13.universalimageloader.core.ImageLoader;


import java.text.FieldPosition;

import java.util.List;


public class MyAdapter extends BaseAdapter {

    private Context context;

    private List<DataInfo> list;


public MyAdapter(Context context, List<DataInfo> list) {

        this.context = context;

        this.list = list;

    }


private final int TYPE_1 = 0;

    private final int TYPE_2 = 1;

    private final int TYPE_3 = 2;


@Override

    public int getItemViewType(int position) {

        if (list.get(position).image_list.size() > 0) {

            return TYPE_1;

        } else if (TextUtils.isEmpty(list.get(position).middle_image.url)) {

            return TYPE_2;

        } else return TYPE_3;


}


@Override

    public int getViewTypeCount() {

        return 3;

    }


@Override

    public int getCount() {

        return list.size();

    }


@Override

    public Object getItem(int i) {

        return i;

    }


@Override

    public long getItemId(int i) {

        return 0;

    }


@Override

    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder1 v1 = null;

        ViewHolder2 v2 = null;

        ViewHolder3 v3 = null;

        int type = getItemViewType(i);

        if (view == null) {

            switch (type) {

                case TYPE_1:

                    view = View.inflate(context, R.layout.item3, null);

                    v1 = new ViewHolder1();

                    v1.image1 = (ImageView) view.findViewById(R.id.image1);

                    v1.image2 = (ImageView) view.findViewById(R.id.image2);

                    v1.image3 = (ImageView) view.findViewById(R.id.image3);

                    v1.t1 = (TextView) view.findViewById(R.id.t1);

                    view.setTag(v1);

                    break;

                case TYPE_2:

                    view = View.inflate(context, R.layout.item2, null);

                    v2 = new ViewHolder2();

                    v2.image4 = (ImageView) view.findViewById(R.id.image4);

                    v2.t2 = (TextView) view.findViewById(R.id.t2);

                    view.setTag(v2);

                    break;

                case TYPE_3:

                    view = View.inflate(context, R.layout.item1, null);

                    v3 = new ViewHolder3();

                    v3.t3 = (TextView) view.findViewById(R.id.t3);

                    view.setTag(v3);

                    break;

            }

        } else {

            switch (type) {

                case TYPE_1:

                    v1 = (ViewHolder1) view.getTag();

                    break;

                case TYPE_2:

                    v2 = (ViewHolder2) view.getTag();

                    break;

                case TYPE_3:

                    v3 = (ViewHolder3) view.getTag();

                    break;

            }

        }

        switch (type) {

            case TYPE_1:

                ImageLoader.getInstance().displayImage(list.get(i).image_list.get(0).url, v1.image1);

                ImageLoader.getInstance().displayImage(list.get(i).image_list.get(1).url, v1.image2);

                ImageLoader.getInstance().displayImage(list.get(i).image_list.get(2).url, v1.image3);

                v1.t1.setText(list.get(i).title);

                break;

            case TYPE_2:

                ImageLoader.getInstance().displayImage(list.get(i).middle_image.url,v2.image4);

                v2.t2.setText(list.get(i).title);

                break;

            case TYPE_3:

                v3.t3.setText(list.get(i).title);

                break;

        }

        return view;

    }


class ViewHolder1 {

        TextView t1;

        ImageView image1, image2, image3;

    }


class ViewHolder2 {

        TextView t2;

        ImageView image4;

    }


class ViewHolder3 {

        TextView t3;

    }


}


//解析json串


import android.content.Intent;

import android.os.AsyncTask;

import android.os.Handler;

import android.os.Message;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;


import com.bawei.Yuekao_exam_three.R;

import com.bawei.Yuekao_exam_three.adapter.MyAdapter;

import com.bawei.Yuekao_exam_three.bean.DataInfo;

import com.bawei.Yuekao_exam_three.bean.User;

import com.google.gson.Gson;


import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;


import cz.msebera.android.httpclient.HttpResponse;

import cz.msebera.android.httpclient.client.HttpClient;

import cz.msebera.android.httpclient.client.methods.HttpGet;

import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;

import xlistview.bawei.com.xlistviewlibrary.XListView;


public class MainActivity extends AppCompatActivity {

    private XListView xlv;

    int p = 1;

    Handler handler=new Handler();

    List<DataInfo> list1=new ArrayList<>();

    private List<DataInfo> list;

    /* Handler handler = new Handler() {

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

            if (msg.what == 0) {

                String json = (String) msg.obj;

                Gson gson = new Gson();

                User user = gson.fromJson(json, User.class);

                List<DataInfo> list = user.data;

                xlv.setAdapter(new MyAdapter(MainActivity.this,list));

            }

        }

    };*/


@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        xlv = (XListView) findViewById(R.id.xlv);

        //獲取網絡數據

        getServerData();


xlv.setPullLoadEnable(true);

        xlv.setPullRefreshEnable(true);

        xlv.setXListViewListener(new XListView.IXListViewListener() {

            @Override

            public void onRefresh() {

                handler.postDelayed(new Runnable() {

                    @Override

                    public void run() {

                        showTime();

                        p=0;

                        getServerData();

                        xlv.stopRefresh();

                    }

                },2000);

            }


@Override

            public void onLoadMore() {

                handler.postDelayed(new Runnable() {

                    @Override

                    public void run() {

                        p++;

                        getServerData();

                        xlv.stopLoadMore();

                    }

                },2000);

            }

        });


}


private void showTime() {

        xlv.stopLoadMore();

        xlv.stopRefresh();

        long timeMillis = System.currentTimeMillis();

        SimpleDateFormat simp=new SimpleDateFormat("yy-MM--dd hh:mm:ss");

        Date date=new Date(timeMillis);

        String time = simp.format(date);

        xlv.setRefreshTime(time);

    }


private void getServerData() {

        MyAsyncTask my=new MyAsyncTask();

        my.execute();

       /* new Thread() {

            @Override

            public void run() {

                super.run();

                try {

                    URL url = new URL(urlJson);

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setConnectTimeout(5000);

                    conn.setRequestMethod("GET");

                    if (conn.getResponseCode() == 200) {

                        InputStream inputStream = conn.getInputStream();

                        byte[] b = new byte[1024];

                        int len = 0;

                        ByteArrayOutputStream bos = new ByteArrayOutputStream();

                        while ((len = inputStream.read(b)) != -1) {

                            bos.write(b, 0, len);

                        }

                        bos.close();

                        inputStream.close();

                        String json = bos.toString("utf-8");

                        Message message = Message.obtain();

                        message.what = 0;

                        message.obj = json;

                        handler.sendMessage(message);

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        }.start();*/

    }

    class MyAsyncTask extends AsyncTask<String,Integer,String>{


@Override

        protected String doInBackground(String... strings) {

            String urlJson = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api="

+ p;

            HttpClient client=new DefaultHttpClient();

            HttpGet result=new HttpGet(urlJson);

            try {

                HttpResponse response = client.execute(result);

                if(response.getStatusLine().getStatusCode()==200){

                    InputStream inputStream = response.getEntity().getContent();

                    byte[] b = new byte[1024];

                    int len = 0;

                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    while ((len = inputStream.read(b)) != -1) {

                        bos.write(b, 0, len);

                    }

                    bos.close();

                    inputStream.close();

                    String json = bos.toString("utf-8");

                    return json;

                }

            } catch (IOException e) {

                e.printStackTrace();

            }

            return null;

        }


@Override

        protected void onPostExecute(String s) {

            super.onPostExecute(s);

            Gson gson = new Gson();

            User user = gson.fromJson(s, User.class);

            list = user.data;

            list1.addAll(list);

            xlv.setAdapter(new MyAdapter(MainActivity.this,list1));

            //監聽item

            xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override

                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                    Intent it=new Intent(MainActivity.this,OneActivity.class);

                    it.putExtra("url", list.get(i).article_url);

                    startActivity(it);

                    finish();

                }

            });

        }

    }

}

0 個評論

要回覆文章請先登錄註冊