com.tk.httpClientErp.headmandraw.DrawsListActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.tk.httpClientErp.headmandraw.DrawsListActivity.java

Source

package com.tk.httpClientErp.headmandraw;

import java.util.ArrayList;
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.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 DrawsListActivity extends ListActivity {

    private boolean mResult = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String drawData = getIntent().getStringExtra("drawData");
        List<HashMap<String, Object>> draws = jsonToListMap(drawData);
        MyStore.drawList = draws;
        String[] from = new String[] { "headman", "baoyang", "worksite", "project", "gd_no", "license", "identifi",
                "busno", "busmodel", "in_time", "inspect", "mResult", "bus_no" };
        int[] to = new int[] { R.id.draw_list_groupleader, R.id.draw_list_maintenance, R.id.draw_list_worksite,
                R.id.draw_list_project, R.id.draw_list_orderno, R.id.draw_list_License,
                R.id.draw_list_identification, R.id.draw_list_busno, R.id.draw_list_busmodel,
                R.id.draw_list_admissiontime, R.id.draw_list_inspectiontime, R.id.draw_list_checkbox,
                R.id.draw_list_bus_no };
        MySimpleAdapter adapter = new MySimpleAdapter(this, MyStore.drawList, R.layout.activity_drawlist, from, to);
        adapter.setWhichCheckbox(R.id.draw_list_checkbox);
        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);
            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 headman = busObject.getString("headman_name"); // 
                String baoyang = busObject.getString("workgroup_name"); // 
                String worksite = busObject.getString("space_name"); // 
                String project = busObject.getString("xm_name"); // 
                String gd_no = busObject.getString("gd_no"); // 
                String License = busObject.getString("license"); // 
                String identifi = busObject.getString("bus_id"); // 
                String busno = busObject.getString("bus_no"); // 
                String busmodel = busObject.getString("bus_type_name"); // 
                String in_time = busObject.getString("in_time"); // 
                String inspect = busObject.getString("initial_check_time"); // 
                String bus_no = busObject.getString("bus_no");

                busHashMap.put("bus_no", bus_no);
                busHashMap.put("headman", headman);
                busHashMap.put("baoyang", baoyang);
                busHashMap.put("worksite", worksite);
                busHashMap.put("project", project);
                busHashMap.put("gd_no", gd_no);
                busHashMap.put("license", License);
                busHashMap.put("identifi", identifi);
                busHashMap.put("busno", busno);
                busHashMap.put("busmodel", busmodel);
                busHashMap.put("in_time", in_time);
                busHashMap.put("inspect", inspect);
                busHashMap.put("mResult", false); // checkbox
                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>();
        switch (item.getItemId()) // Id
        {
        case R.id.menu_item_buslist_yes:
            HashMap<String, Object> hashMapsYES = MyStore.subAndDel(MyStore.drawList);
            List<JSONObject> submitJsonArrayYES = (List<JSONObject>) hashMapsYES.get("submitJsonArray");
            List<HashMap<String, Object>> delListYES = (List<HashMap<String, Object>>) hashMapsYES.get("delList");
            params.add(new BasicNameValuePair("submitJsonArray", submitJsonArrayYES.toString()));
            String reultString = null;
            try {
                reultString = HttpClientUtil.postRequest("/android.do?method=updateDraws", params,
                        MyStore.sessionID);
            } catch (TimeoutException e) {
                e.printStackTrace();
                DrawsListActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DrawsListActivity.this, MyStore.TIMEOUTLOGIN, Toast.LENGTH_SHORT).show();
                    }
                });
            }
            reultString = HttpClientUtil.callBackSuccOrFail(reultString, "resualt");
            if (HttpClientUtil.reSUCCorFAILE)
                MyStore.drawList.removeAll(delListYES);
            final String MSG = reultString;
            DrawsListActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(DrawsListActivity.this, MSG, Toast.LENGTH_SHORT).show();
                }
            });
            ((MySimpleAdapter) getListAdapter()).notifyDataSetChanged();
            break;
        case R.id.menu_item_buslist_selectec_or_cancle:
            MyStore.selectORcancle(mResult, "mResult", MyStore.drawList);
            mResult = mResult == true ? false : true;
            ((MySimpleAdapter) getListAdapter()).notifyDataSetChanged();
            break;
        }
        return true;
    }

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

}