xlistview

来源:互联网 发布:mac怎么重置网络设置 编辑:程序博客网 时间:2024/04/29 13:32
package com.example.shixunlianxi;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.example.adapter.MyListView;
import com.example.bean.Bean;
import com.example.bean.Bean.DataBean;
import com.example.xlistview.XListView;
import com.example.xlistview.XListView.IXListViewListener;
import com.google.gson.Gson;

import android.content.Entity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;

public class MyFragment extends Fragment {
    private XListView xListView;
    private List<DataBean> list;
    private MyListView mlv;
    private String path;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Bean bean = (Bean) msg.obj;
            list = bean.getData();
            System.out.println(list + "0000000000000");

            xListView.setAdapter(new MyListView(list, getActivity()));
            xListView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    DataBean dataBean = list.get(position - 1);
                    Intent intent = new Intent(getActivity(),
                            WebviewActivity.class);
                    intent.putExtra("url", dataBean.getUrl());
                    startActivity(intent);

                }
            });

            xListView.setPullLoadEnable(true);
            xListView.setPullRefreshEnable(true);
            xListView.setXListViewListener(new IXListViewListener() {
                // 下拉刷新
                @Override
                public void onRefresh() {
                    list.clear();

                    new Thread() {
                        public void run() {
                            getdata();
                        }
                    }.start();
                    mlv.notifyDataSetChanged();
                    xListView.stopLoadMore();
                    xListView.stopRefresh();
                    xListView.setRefreshTime(time());

                }

                // 上拉加载
                @Override
                public void onLoadMore() {
                    mlv.notifyDataSetChanged();
                    xListView.stopLoadMore();
                    xListView.stopRefresh();

                }
            });

        };
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.xlistview, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

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

        path = "http://ic.snssdk.com/2/article/v25/stream/?category=";
        Bundle bundle = getArguments();
        String type = bundle.getString("name");
        /*
         * if (type.equals("推荐")) { path =
         * "http://ic.snssdk.com/2/article/v25/stream/?category="; } else { path
         * = "http://ic.snssdk.com/2/article/v25/stream/?category=" + type; }
         */

        new Thread() {
            public void run() {
                getdata();
            }
        }.start();

    }

    private void getdata() {
        /*
         * Bundle bundle = getArguments(); String type =
         * bundle.getString("name");
         */
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(path);
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entity = response.getEntity();

                String string = EntityUtils.toString(entity);
                System.out.println(string + "222222222222222");
                Gson gson = new Gson();
                Bean bean = gson.fromJson(string, Bean.class);
                Message message = Message.obtain();
                message.obj = bean;
                handler.sendMessage(message);
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    private String time() {
        long mi = System.currentTimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String string = format.format(mi);

        return string;
    }

}

0 0
原创粉丝点击