Java tutorial
package com.tk.httpClientErp.buscheck; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; 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.annotation.SuppressLint; import android.app.ListActivity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListView; 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; @SuppressLint("SimpleDateFormat") public class BusListActivity extends ListActivity { private boolean mResult = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String mBusData = getIntent().getStringExtra("busData"); MyStore.busList = jsonToListMap(mBusData); String[] from = new String[] { "lineName", "license", "mResult" }; int[] to = new int[] { R.id.tv_lineName, R.id.tv_license, R.id.bus_list_pass }; MySimpleAdapter adapter = new MySimpleAdapter(this, MyStore.busList, R.layout.activity_checkbuslist, from, to); adapter.setWhichCheckbox(R.id.bus_list_pass); adapter.setWhichParam("mResult"); setListAdapter(adapter); } /** * 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); JSONArray busArray = new JSONArray(jo.getString("data")); for (int i = 0; i < busArray.length(); i++) { JSONObject busObject = busArray.getJSONObject(i); HashMap<String, Object> busHashMap = new HashMap<String, Object>(); String busID = busObject.getString("bus_id"); String lineNameString = busObject.getString("line_name"); String licenseString = busObject.getString("license"); String checkMile = busObject.getString("total_mile");// checkMile = checkMile.equals("null") ? "-" : checkMile; String securityCheckDate = busObject.getString("last_security_check_date");// securityCheckDate = securityCheckDate.equals("null") ? "-" : securityCheckDate.substring(0, 10); String deptName = busObject.getString("dept_name"); busHashMap.put("busID", busID); busHashMap.put("checkMile", checkMile); busHashMap.put("securityCheckDate", securityCheckDate); busHashMap.put("lineName", lineNameString); busHashMap.put("license", licenseString); busHashMap.put("deptName", deptName); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String currTime = sdf.format(new Date()); busHashMap.put("checkDate", currTime); busHashMap.put("direction", false);// busHashMap.put("draglink", false);// busHashMap.put("tierod", false);// busHashMap.put("knucklearm", false);// busHashMap.put("fourwheelbraking", false);// busHashMap.put("parkingbraking", false);// busHashMap.put("pump", false);// busHashMap.put("gascircuit", false);// busHashMap.put("airbottle", false);// busHashMap.put("gaspipeline", false);// busHashMap.put("maincontrolvalve", false);// busHashMap.put("Halfshaftscrew", false);// busHashMap.put("tyrewhorl", false);// busHashMap.put("Driveshaftscrews", false);// busHashMap.put("bracketScrews", false);// busHashMap.put("mResult", false);// busList.add(busHashMap); } } catch (JSONException e) { e.printStackTrace(); Toast.makeText(BusListActivity.this, MyStore.ERRORJSON, Toast.LENGTH_SHORT).show(); } 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>(); switch (item.getItemId()) { // case R.id.menu_item_buslist_yes: HashMap<String, Object> busListHashMaps = MyStore.subAndDel(MyStore.busList); List<JSONObject> submitJsonArray = (List<JSONObject>) busListHashMaps.get("submitJsonArray"); List<HashMap<String, Object>> delList = (List<HashMap<String, Object>>) busListHashMaps.get("delList"); params.add(new BasicNameValuePair("submitJsonArray", submitJsonArray.toString())); String reultString = null; try { reultString = HttpClientUtil.postRequest("/android.do?method=checkBusList", params, MyStore.sessionID); } catch (TimeoutException e) { e.printStackTrace(); BusListActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(BusListActivity.this, MyStore.TIMEOUTLOGIN, Toast.LENGTH_SHORT).show(); } }); } if (HttpClientUtil.reSUCCorFAILE && delList.size() != 0) MyStore.busList.removeAll(delList); reultString = HttpClientUtil.callBackSuccOrFail(reultString, "resualt"); final String MSG = reultString; BusListActivity.this.runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(BusListActivity.this, MSG, Toast.LENGTH_SHORT).show(); } }); ((SimpleAdapter) getListAdapter()).notifyDataSetChanged(); break; // / case R.id.menu_item_buslist_selectec_or_cancle: MyStore.selectORcancle(mResult, "mResult", MyStore.busList); mResult = mResult == true ? false : true; ((SimpleAdapter) getListAdapter()).notifyDataSetChanged(); break; } return true; } @SuppressWarnings("unchecked") @Override public void onListItemClick(ListView l, View v, int position, long id) { HashMap<String, Object> busBean = (HashMap<String, Object>) getListAdapter().getItem(position); Intent intent = new Intent(BusListActivity.this, BusDetailActivity.class); intent.putExtra("busID", busBean.get("busID").toString()); startActivity(intent); } @Override public void onResume() { super.onResume(); ((SimpleAdapter) getListAdapter()).notifyDataSetChanged(); } }