package com.exmple.frage; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import com.bawei.shuaxi.PullToRefreshLayout; import com.bawei.shuaxi.PullToRefreshLayout.OnRefreshListener; import com.bawei.shuaxi.PullableListView; import com.exmple.apa.NewShuju; import com.exmple.help.MyHelp; import com.exmple.jinritoutiao.R; import com.exmple.jinritoutiao.XiangqingActivity; import com.exmple.net.NetUtil; import com.exmple.tools.Urlclass; import com.exmple.vo.MyData; import com.exmple.vo.Super; import com.google.gson.Gson; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; 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.ListView; import android.widget.TextView; import android.widget.Toast; public class Fragment_2 extends Fragment { Handler ha = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 1) { String ma = (String) msg.obj; Gson gs = new Gson(); Super fromJson = gs.fromJson(ma, Super.class); list = fromJson.getData(); lists = list; update(); } if (msg.what == 2) { String ma = (String) msg.obj; Gson gs = new Gson(); Super fromJson = gs.fromJson(ma, Super.class); list = fromJson.getData(); lists.addAll(list); update(); } // lists.addAll(list); listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent ints = new Intent(getActivity(), XiangqingActivity.class); ints.putExtra("info1", list.get(position).getUrl()); ints.putExtra("im", list.get(position).getDisplay_url()); ints.putExtra("title", list.get(position).getTitle()); startActivity(ints); } }); } }; int page=0; private TextView category; private List<MyData> list; private List<MyData> lists = new ArrayList<MyData>(); private PullableListView listview; private NewShuju ne; private SQLiteDatabase sql; private View view; private PullToRefreshLayout layout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (view == null) { view = inflater.inflate(R.layout.news_fragment, null); layout = (PullToRefreshLayout) view.findViewById(R.id.layout); } ViewGroup vp = (ViewGroup) view.getParent(); if (vp != null) { vp.removeAllViews(); } MyHelp myHelp = new MyHelp(getActivity(), "navs", null, 1); sql = myHelp.getWritableDatabase(); listview = (PullableListView) view.findViewById(R.id.listview1); category = (TextView) view.findViewById(R.id.category_line); Date(); setJson(); layout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) { ha.postDelayed(new Runnable() { @Override public void run() { setJson(); pullToRefreshLayout .refreshFinish(PullToRefreshLayout.SUCCEED); } }, 1000); pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED); } @Override public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) { ha.postDelayed(new Runnable() { @Override public void run() { // setJson(); Bundle b = getArguments(); if (b != null) { String string1 = b.getString("text"); System.out.println(string1 + "0000000"); // 調用方法 HashMap<String, String> urls = Urlclass.getUrl(); // 獲取對應的 URL final String url = urls.get(string1); new Thread() { public void run() { String str1 = NetUtil.getUrl(url+page); ha.sendMessage(ha.obtainMessage(2, str1)); }; }.start(); } // 千萬別忘了告訴控件加載完畢了哦! pullToRefreshLayout .loadmoreFinish(PullToRefreshLayout.SUCCEED); } }, 1000); } }); return view; } private void setJson() { Bundle b = getArguments(); if (b != null) { String string1 = b.getString("text"); System.out.println(string1 + "0000000"); // 調用方法 HashMap<String, String> urls = Urlclass.getUrl(); // 獲取對應的 URL final String url = urls.get(string1); new Thread() { public void run() { String str1 = NetUtil.getUrl(url); ha.sendMessage(ha.obtainMessage(1, str1)); }; }.start(); } } private void Date() { Calendar c = Calendar.getInstance(); // 獲取當前年份 String mYear = String.valueOf(c.get(Calendar.YEAR)); // 獲取當前月份 String mMonth = String.valueOf(c.get(Calendar.MONTH) + 1); // 獲取當前月份的日期號碼 String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH)); // 獲取當前星期 他獲得的是數字1 2 3 4 5 6所以根據數字換一下 String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK)); if ("1".equals(mWay)) { mWay = "天"; } else if ("2".equals(mWay)) { mWay = "一"; } else if ("3".equals(mWay)) { mWay = "二"; } else if ("4".equals(mWay)) { mWay = "三"; } else if ("5".equals(mWay)) { mWay = "四"; } else if ("6".equals(mWay)) { mWay = "五"; } else if ("7".equals(mWay)) { mWay = "六"; } category.setText(mYear + "." + mMonth + "." + mDay + " 星期" + mWay + " " + "今天"); } private void update() { if (ne == null) { ne = new NewShuju(list, getActivity(), listview); listview.setAdapter(ne); } else { ne.UpDataAdapter(lists); } }; }