package com.example.fragment;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.example.adapter.MybaseAdatpter;
import com.example.jinritoutiao.R;
import com.example.jinritoutiao.XiangqingActivity;
import com.example.network.NetWorkUtil;
import com.example.view.XListView;
import com.example.view.XListView.IXListViewListener;
import com.example.vo.Mydata;
import com.example.vo.Supper;
import com.google.gson.Gson;
import com.lidroid.xutils.http.client.RetryHandler;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MyFragment extends Fragment implements IXListViewListener {
private String st;
private String url;
private List<Mydata> list;
private List<Mydata> lists=new ArrayList<Mydata>();;
private ProgressDialog progressDialog = null;
public MyFragment(String st, String url) {
super();
this.st = st;
this.url = url;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.activity_fragment, null);
TextView textView = (TextView) view.findViewById(R.id.tt);
textView.setText(st);
listView = (XListView) view.findViewById(R.id.fragment_listview);
// progressDialog = ProgressDialog.show(getActivity(), "请稍等...",
// "加载中...",true);
listView.setPullLoadEnable(true);
listView.setPullRefreshEnable(true);
listView.setXListViewListener(this);
if (NetWorkUtil.IsNetWork(getActivity()) == 1) {
// 请求网络数据
Qingqiujson();
return view;
}
if (NetWorkUtil.IsNetWork(getActivity()) == 2) {
AlertDialog.Builder builder = new Builder(getActivity());
builder.setMessage("非WIFI下观看");
builder.setTitle("提示");
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Qingqiujson();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.create().show();
return view;
}
if (NetWorkUtil.IsNetWork(getActivity()) == 3) {
Toast.makeText(getActivity(), "没有网络", 0).show();
return view;
}
return view;
}
private void Qingqiujson() {
new Thread() {
public void run() {
String sttt = NetWorkUtil.httpclient_get(url);
if (sttt.equals("")) {
handler.sendEmptyMessage(7);
} else if (sttt != "") {
Gson gson = new Gson();
Supper supper = gson.fromJson(sttt, Supper.class);
list = supper.getData();
lists.addAll(0,list);
handler.sendMessage(handler.obtainMessage(1, lists));
}
};
}.start();
}
private void Qingqiujson1() {
new Thread() {
public void run() {
String sttt = NetWorkUtil.httpclient_get(url);
if (sttt.equals("")) {
handler.sendEmptyMessage(7);
} else if (sttt != "") {
Gson gson = new Gson();
Supper supper = gson.fromJson(sttt, Supper.class);
list = supper.getData();
lists.addAll(list);
handler.sendEmptyMessage(10);
}
};
}.start();
}
Handler handler = new Handler() {
private MybaseAdatpter adatpter;
public void handleMessage(android.os.Message msg) {
// progressDialog.dismiss();
// 设置时间;
onLoad();
if (msg.what == 10) {
adatpter.notifyDataSetChanged();
}
if (msg.what == 7) {
Toast.makeText(getActivity(), "连接超时", 0).show();
return;
}
if (msg.what == 1) {
adatpter = new MybaseAdatpter(lists, getActivity());
listView.setAdapter(adatpter);
}
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),
XiangqingActivity.class);
if (lists.get(position).getShare_url() != null) {
intent.putExtra("url", lists.get(position - 1)
.getShare_url());
startActivity(intent);
return;
} else {
intent.putExtra("url", lists.get(position - 1)
.getArticle_url());
startActivity(intent);
}
}
});
};
};
private XListView listView;
@Override
public void onRefresh() {
// TODO Auto-generated method stub
Qingqiujson();
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
Qingqiujson1();
}
/*
*
* 设置时间
*/
private void onLoad() {
// 停止刷新、加载
listView.stopRefresh();
listView.stopLoadMore();
// 设置日期格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 获取当前系统时间
String nowTime = df.format(new Date(System.currentTimeMillis()));
// 释放时提示正在刷新时的当前时间
listView.setRefreshTime(nowTime);
}
}