Java tutorial
package com.tk.httpClientErp.wsHeadCheck; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.annotation.SuppressLint; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.SimpleAdapter; import com.tk.MyStore; import com.tk.httpClientErp.R; /** * * @author Administrator * */ public class WsHeadCheckList extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String mWsHeadCheckData = getIntent().getStringExtra("mWsHeadCheckData"); List<HashMap<String, Object>> headManCheackList = jsonToListMap(mWsHeadCheckData); MyStore.wsheadCheckList = headManCheackList; MyStore.wsheadCheckFenZuList = classification(headManCheackList); //List String[] from = new String[] { "detail", "gd_no", "license", "xm_name", "bus_no" }; int[] to = new int[] { R.id.wsheadcheck_detail, R.id.wsheadchecklist_gd_no, R.id.wsheadchecklist_licence, R.id.wsheadchecklist_project, R.id.wsheadchecklist_bus_no }; SimpleAdapter adapter = new SimpleAdapter(this, MyStore.wsheadCheckFenZuList, R.layout.activity_wsheadchecklist, from, to); setListAdapter(adapter); } /** * json,in_time * @return */ private List<HashMap<String, Object>> classification(List<HashMap<String, Object>> jsontoList) { boolean initCompare = true; //true HashMap<String, Object> compareContainer = null; // List<HashMap<String, Object>> fenzuList = new ArrayList<HashMap<String, Object>>(); // if (jsontoList.size() == 0) return fenzuList; // HashSet<String> gd_no = new HashSet<String>(); for (HashMap<String, Object> bean : jsontoList) { gd_no.add(bean.get("gd_no").toString()); } //in_time for (String gdstr : gd_no) { compareContainer = new HashMap<String, Object>(); for (HashMap<String, Object> bean : jsontoList) { if (gdstr.equals(bean.get("gd_no").toString()) && initCompare) { compareContainer = bean; initCompare = false; } else if (gdstr.equals(bean.get("gd_no").toString()) && compareTime(bean.get("in_time").toString(), compareContainer.get("in_time").toString())) { compareContainer = bean; } } fenzuList.add(compareContainer); initCompare = true; } return fenzuList; } /* * */ @SuppressLint("SimpleDateFormat") private boolean compareTime(String ComTime, String ToTime) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date1 = new Date(); Date date2 = new Date(); try { date1 = sdf.parse(ComTime); date2 = sdf.parse(ToTime); } catch (ParseException e) { e.printStackTrace(); } return date1.before(date2); } /** * JsonList<HashMap<String,Object>> * * @param strJsion * @return List<HashMap<String,Object>> * @throws JSONException */ private List<HashMap<String, Object>> jsonToListMap(String strJsion) { List<HashMap<String, Object>> busList = new ArrayList<HashMap<String, Object>>(); try { JSONObject jo = new JSONObject(strJsion); String jsArrayString = jo.getString("data"); JSONArray busArray = new JSONArray(jsArrayString); for (int i = 0; i < busArray.length(); i++) { JSONObject busObject = busArray.getJSONObject(i); HashMap<String, Object> busHashMap = new HashMap<String, Object>(); String gd_no = busObject.getString("gd_no"); // String detail = busObject.getString("detail"); // String gw_name = busObject.getString("gw_name"); // String headman_name = busObject.getString("headman_name"); // String workgroup_name = busObject.getString("workgroup_name"); // String gs = busObject.getString("gs"); // String license = busObject.getString("license"); String worker_name = busObject.getString("worker_name"); // String start_time = busObject.getString("start_time"); // String re_flag = busObject.getString("re_cnt"); // String check_time = busObject.getString("check_time"); // String finish_time = busObject.getString("finish_time"); // String gw_no = busObject.getString("gw_no"); String in_time = busObject.getString("in_time"); // String workgroup_id = busObject.getString("workgroup_id"); //update String xm_name = busObject.getString("xm_name"); String bus_no = busObject.getString("bus_no"); busHashMap.put("bus_no", bus_no); busHashMap.put("xm_name", xm_name); busHashMap.put("license", license); busHashMap.put("gd_no", gd_no); busHashMap.put("gw_no", gw_no); busHashMap.put("worker_name", worker_name); busHashMap.put("detail", detail); busHashMap.put("gw_name", gw_name); busHashMap.put("headman_name", headman_name); busHashMap.put("workgroup_name", workgroup_name); busHashMap.put("gs", gs); busHashMap.put("start_time", start_time); busHashMap.put("re_flag", re_flag); busHashMap.put("finish_time", finish_time); busHashMap.put("check_time", check_time); busHashMap.put("in_time", in_time); busHashMap.put("workgroup_id", workgroup_id); busHashMap.put("mResult", false); // checkbox busList.add(busHashMap); } } catch (JSONException e) { e.printStackTrace(); } return busList; } //id @SuppressWarnings("unchecked") @Override public void onListItemClick(ListView l, View v, int position, long id) { HashMap<String, Object> wsHeadCheckBean = (HashMap<String, Object>) getListAdapter().getItem(position); Intent intent = new Intent(WsHeadCheckList.this, WsHeadCheckDetail.class); intent.putExtra("gd_no", wsHeadCheckBean.get("gd_no").toString()); startActivity(intent); } @Override public void onResume() { super.onResume(); ((SimpleAdapter) getListAdapter()).notifyDataSetChanged(); } }