com.tk.httpClientErp.headmancheck.HeadmancheckListActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.tk.httpClientErp.headmancheck.HeadmancheckListActivity.java

Source

package com.tk.httpClientErp.headmancheck;

import java.util.ArrayList;
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.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;

public class HeadmancheckListActivity extends ListActivity {
    @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", "bus_no" };
        int[] to = new int[] { R.id.headmancheck_list_orderno, R.id.headmancheck_list_project,
                R.id.headmancheck_list_License, R.id.headmancheck_list_fix, R.id.headmancheck_list_maintenance,
                R.id.headmancheck_list_bus_no };
        SimpleAdapter adapter = new SimpleAdapter(this, MyStore.headManCheckFenzu,
                R.layout.activity_headmanchecklist, from, to);
        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;
                }
            }
            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 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("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("mResult", false);
                busList.add(busHashMap);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return busList;
    }

    @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(HeadmancheckListActivity.this, HeadmancheckDetailActivity.class);
        intent.putExtra("gd_no", wsHeadCheckBean.get("gd_no").toString());
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        ((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
    }
}