Java tutorial
package com.tk.httpClientErp.headmancheck; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.concurrent.TimeoutException; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.SimpleAdapter; import android.widget.Toast; import com.tk.MyStore; import com.tk.httpClientErp.R; import com.tk.netUtil.HttpClientUtil; import com.tk.netUtil.MySimpleAdapter; public class Headmancheckqianshuzhi extends ListActivity { private boolean mResult = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String mHeanmancheckData = getIntent().getStringExtra("heanmancheckData"); List<HashMap<String, Object>> headManCheackList = jsonToListMap(mHeanmancheckData); MyStore.headManCheckList = headManCheackList; MyStore.headManCheckFenzu = classification(headManCheackList); String[] from = new String[] { "gd_no", "xmname", "license", "workername", "baoyang", "qianshuzhi_qian", "qianshuzhi_hou", "mResult", "bus_no" }; int[] to = new int[] { R.id.headmancheckqianshuzhi_orderno, R.id.headmancheckqianshuzhi_project, R.id.headmancheckqianshuzhi_License, R.id.headmancheckqianshuzhi_fix, R.id.headmancheckqianshuzhi_maintenance, R.id.headmancheckqianshuzhi_before, R.id.headmancheckqianshuzhi_after, R.id.headmancheckqianshuzhi_checkbox, R.id.headmancheckqianshuzhi_bus_no }; MySimpleAdapter adapter = new MySimpleAdapter(this, MyStore.headManCheckFenzu, R.layout.activity_headmancheckqianshuzhi, from, to); adapter.setWhichEditText_qian(R.id.headmancheckqianshuzhi_before); adapter.setWhichEditText_hou(R.id.headmancheckqianshuzhi_after); adapter.setWhichCheckbox(R.id.headmancheckqianshuzhi_checkbox); adapter.setWhichParam("mResult"); setListAdapter(adapter); } /** * json * * @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()); } // 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())) { compareContainer = bean; } } compareContainer.put("mResult", false); fenzuList.add(compareContainer); initCompare = true; } return fenzuList; } /** * JsionList<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 baoyang = busObject.getString("workgroup_name"); // String workername = busObject.getString("worker_name"); // String gwname = busObject.getString("gw_name"); // String gs = busObject.getString("gs"); // String workgoupid = busObject.getString("workgroup_id"); // String re_cnt = busObject.getString("re_cnt"); // String groupcat_name = busObject.getString("groupcat_name"); // String starttime = busObject.getString("start_time"); // String finishtime = busObject.getString("finish_time"); // String xmname = busObject.getString("xm_name"); // String gd_no = busObject.getString("gd_no"); // String license = busObject.getString("license"); // String bustypename = busObject.getString("bus_type_name"); // String intime = busObject.getString("in_time"); // String op_no = busObject.getString("op_no"); String gw_no = busObject.getString("gw_no"); // String headman_opno = busObject.getString("headman_opno"); // String bus_no = busObject.getString("bus_no"); busHashMap.put("bus_no", bus_no); busHashMap.put("groupcat_name", groupcat_name); busHashMap.put("re_cnt", re_cnt); busHashMap.put("op_no", op_no); busHashMap.put("baoyang", baoyang); busHashMap.put("workername", workername); busHashMap.put("gwname", gwname); busHashMap.put("gs", gs); busHashMap.put("workgoupid", workgoupid); busHashMap.put("starttime", starttime); busHashMap.put("finishtime", finishtime); busHashMap.put("xmname", xmname); busHashMap.put("gd_no", gd_no); busHashMap.put("license", license); busHashMap.put("bustypename", bustypename); busHashMap.put("intime", intime); busHashMap.put("gw_no", gw_no); busHashMap.put("headman_opno", headman_opno); busHashMap.put("qianshuzhi_qian", ""); busHashMap.put("qianshuzhi_hou", ""); busHashMap.put("headman_opno", headman_opno); if (busHashMap.get("groupcat_name").toString().contains("")) { busList.add(busHashMap); } } } catch (JSONException e) { e.printStackTrace(); } return busList; } /** * ========================================= */ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.buslist, menu); return true; } /** * ===================================== */ @SuppressWarnings("unchecked") @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); List<NameValuePair> params = new ArrayList<NameValuePair>(); String reultString = null; switch (item.getItemId()) { case R.id.menu_item_buslist_yes: HashMap<String, Object> hashMapsYES = MyStore.subAndDel(MyStore.headManCheckFenzu); List<JSONObject> submitJsonArrayYES = (List<JSONObject>) hashMapsYES.get("submitJsonArray"); params.add(new BasicNameValuePair("submitJsonArray", submitJsonArrayYES.toString())); try { reultString = HttpClientUtil.postRequest("/android.do?method=qianshuzhi", params, MyStore.sessionID); } catch (TimeoutException e) { e.printStackTrace(); Headmancheckqianshuzhi.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(Headmancheckqianshuzhi.this, MyStore.TIMEOUTLOGIN, Toast.LENGTH_SHORT) .show(); } }); } reultString = HttpClientUtil.callBackSuccOrFail(reultString, "resualt"); final String MSG = reultString; Headmancheckqianshuzhi.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(Headmancheckqianshuzhi.this, MSG, Toast.LENGTH_SHORT).show(); } }); case R.id.menu_item_buslist_selectec_or_cancle: MyStore.selectORcancle(mResult, "mResult", MyStore.headManCheckFenzu); mResult = mResult == true ? false : true; ((MySimpleAdapter) getListAdapter()).notifyDataSetChanged(); break; } return true; } @Override public void onResume() { super.onResume(); ((SimpleAdapter) getListAdapter()).notifyDataSetChanged(); } }