com.tk.httpClientErp.materialsrequest.MaterialsrequestFenzu.java Source code

Java tutorial

Introduction

Here is the source code for com.tk.httpClientErp.materialsrequest.MaterialsrequestFenzu.java

Source

package com.tk.httpClientErp.materialsrequest;

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 MaterialsrequestFenzu extends ListActivity {

    private String mMaterialsrequestData;
    private List<HashMap<String, Object>> fenzuList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMaterialsrequestData = getIntent().getStringExtra("mMaterialsrequestData");
        List<HashMap<String, Object>> materialsrequestList = jsonToListMap(mMaterialsrequestData);
        fenzuList = classification(materialsrequestList);
        MyStore.mterialsrequestList = materialsrequestList;
        MyStore.mterialsrequestFenzu = fenzuList;

        String[] from = new String[] { "gd_no", "xm_name", "license", "gz_no", "headman_name", "workgroup_name",
                "bus_id", "bus_no" };
        int[] to = new int[] { R.id.mterialsrequest_fenzu_gd_no, R.id.mterialsrequest_fenzu_xm_name,
                R.id.mterialsrequest_fenzu_license, R.id.mterialsrequest_fenzu_gz_no,
                R.id.mterialsrequest_fenzu_headman_name, R.id.mterialsrequest_fenzu_workgroup_name,
                R.id.mterialsrequest_fenzu_bus_id, R.id.mterialsrequest_fenzu_bus_no };
        SimpleAdapter adapter = new SimpleAdapter(this, MyStore.mterialsrequestFenzu,
                R.layout.activity_materialsrequest_fenzu, 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());
        }
        // 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())) {
                    compareContainer = bean;
                }
            }
            fenzuList.add(compareContainer);
            initCompare = true;
        }
        return fenzuList;
    }

    /**
     * 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 xm_name = busObject.getString("xm_name"); // 
                String license = busObject.getString("license"); // 
                String gz_no = busObject.getString("gz_no"); // 
                //String re_cnt = busObject.getString("re_cnt"); // 
                String headman_name = busObject.getString("headman_name"); // 
                String workgroup_name = busObject.getString("workgroup_name"); // 
                String bus_id = busObject.getString("bus_id"); // 
                String bus_no = busObject.getString("bus_no"); // 
                String bus_type_name = busObject.getString("bus_type_name"); // 
                String in_time = busObject.getString("in_time"); // 
                String start_time = busObject.getString("start_time"); // 
                String fault_no = busObject.getString("fault_no");
                String mileage = busObject.getString("mileage");
                String xm_no = busObject.getString("xm_no");
                String bus_type_no = busObject.getString("bus_type_no");
                //String storage_no = busObject.getString("storage_no");
                //busHashMap.put("storage_no", storage_no);
                busHashMap.put("fault_no", fault_no);
                busHashMap.put("gd_no", gd_no);
                busHashMap.put("xm_name", xm_name);
                busHashMap.put("license", license);
                busHashMap.put("gz_no", gz_no);
                //   busHashMap.put("re_cnt", re_cnt);
                busHashMap.put("headman_name", headman_name);
                busHashMap.put("workgroup_name", workgroup_name);
                busHashMap.put("bus_id", bus_id);
                busHashMap.put("bus_no", bus_no);
                busHashMap.put("bus_type_name", bus_type_name);
                busHashMap.put("in_time", in_time);
                busHashMap.put("start_time", start_time);

                busHashMap.put("mileage", mileage);
                busHashMap.put("bus_type_no", bus_type_no);
                busHashMap.put("xm_no", xm_no);
                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> selecedBean = (HashMap<String, Object>) getListAdapter().getItem(position);
        Intent intent = new Intent(MaterialsrequestFenzu.this, MaterialsrequestList.class);
        intent.putExtra("gd_no", selecedBean.get("gd_no").toString());
        startActivity(intent);
    }

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