public void saveInspection() {
    String exception = "";
    if (edtException.getText().toString().length() > 0) {
      exception = edtException.getText().toString();
    }
    if (exception.length() > 200) {
      Toast.makeText(
              getApplicationContext(),
              "Exception must be less then 200 character.",
              Toast.LENGTH_LONG)
          .show();
      return;
    }
    String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
    inspection.deleteRelatedPestsRecords();
    if (isCleanInspection) {
      m_inspectionPests = new ArrayList<InspectionPest>();
      inspection.evidence = "";
    }

    inspection.trap_number = trapscan_info.number;
    inspection.trap_type_id = trapscan_info.trap_type_id;
    int bait = 0, trap = 0;
    if (!spnBaitCondition.getSelectedItem().toString().equalsIgnoreCase("Bait Conditions")) {
      bait = BaitConditionsInfo.getIdByName(spnBaitCondition.getSelectedItem().toString());
    }
    if (!spnTrapCondition.getSelectedItem().toString().equalsIgnoreCase("Trap Conditions")) {
      trap = TrapConditionsInfo.getIdByName(spnTrapCondition.getSelectedItem().toString());
    }
    inspection.bait_condition_id = bait;
    inspection.trap_condition_id = trap;
    currentDateTimeString =
        Utils.Instance()
            .getFormatedDate(
                currentDateTimeString, "MMM dd, yyyy hh:mm:ss a", "yyyy-MM-dd hh:mm a");
    inspection.exception = exception;
    inspection.removed = isRemoved;
    inspection.scanned_on = currentDateTimeString;
    try {
      inspection.save();
    } catch (ActiveRecordException e) {
      e.printStackTrace();
    }

    AddTraps(m_inspectionPests, inspection);
    // if (m_list.size() == 1) {
    // if (m_list.get(0).count == 0 && m_list.get(0).pest_type_id == 0) {
    // m_list = new ArrayList<InspectionPest>();
    // AddTraps(m_list, inspection);
    // } else {
    // AddTraps(m_list, inspection);
    // }
    // } else {
    // }
  }
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   switch (requestCode) {
     case EVIDENCE_CODE:
       if (resultCode == RESULT_OK) {
         inspection.evidence = data.getStringExtra("evidance");
         // Toast.makeText(getApplicationContext(), evidence,
         // Toast.LENGTH_LONG).show();
       }
       break;
     case CAPTURE_REQUEST_ID:
       if (resultCode == RESULT_OK) {
         String key = data.getStringExtra("KEY");
         @SuppressWarnings("unchecked")
         ArrayList<InspectionPest> arr =
             (ArrayList<InspectionPest>)
                 ((FieldworkApplication) getApplication()).getStoredObject(key);
         ((FieldworkApplication) getApplication()).removeStoredObject(key);
         m_inspectionPests = arr;
       }
       break;
     case MATERIAL_REQUEST_ID:
       if (resultCode == RESULT_OK) {
         m_inspection_material = InspectionMaterial.getAll();
         bindMaterial(m_inspection_material);
       }
       break;
     default:
       break;
   }
 }
  public void AddTraps(ArrayList<InspectionPest> m_list, InspectionInfo inspection) {
    showProgress();
    InspectionInfo.AddInspectionRecord(
        appointment_id,
        m_list,
        inspection,
        new UpdateMUInfoDelegate() {
          @Override
          public void UpdateSuccessFully(ServiceResponse res) {
            if (!res.isError()) {
              if (NetworkConnectivity.isConnected()) {
                InspectionList.Instance()
                    .refreshInspectionList(
                        appointment_id,
                        new UpdateInfoDelegate() {

                          @Override
                          public void UpdateSuccessFully(ServiceResponse res) {
                            gotoback();
                          }

                          @Override
                          public void UpdateFail(String ErrorMessage) {
                            gotoback();
                          }
                        });
              } else {
                gotoback();
              }
            }
          }

          @Override
          public void UpdateFail(String ErrorMessage) {
            hideProgress();
            Toast.makeText(getApplicationContext(), ErrorMessage, Toast.LENGTH_LONG).show();
          }
        });
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.trap_details);
    // info = MainActivity.appointmentInfo;
    Bundle b = getIntent().getExtras();
    if (b != null) {
      appointment_id = b.getInt(Const.Appointment_Id);
      cust_id = b.getInt("CUST_ID");
      barcode = b.getString("BARCODE");
      if (b.containsKey("isFromUnChecked")) {
        isFromUnChecked = b.getBoolean("isFromUnChecked");
      }
    }
    if (appointment_id == 0) {
      appointment_id = Const.app_id;
    }
    if (cust_id == 0) {
      cust_id = Const.customer_id;
    }
    if (barcode.length() == 0) {
      barcode = Const.BarCode;
    }
    action = getSupportActionBar();
    // action.setTitle("Traps Scan Details");
    action.setTitle(
        Html.fromHtml(
            "<font color='"
                + getString(R.string.header_text_color)
                + "'>Traps Scan Details</font>"));
    action.setHomeButtonEnabled(true);
    action.setDisplayHomeAsUpEnabled(true);

    lstMaterial = (ListView) findViewById(R.id.lstMaterialUsages);
    btnSaveTrapData = (Button) findViewById(R.id.btnSaveTrapData);
    btnClean = (Button) findViewById(R.id.btnClean);
    rlEvidence = (RelativeLayout) findViewById(R.id.rlEvidence);
    rlCaptured = (RelativeLayout) findViewById(R.id.rlCaptured);
    llContainer = (LinearLayout) findViewById(R.id.llContainer);
    rlMaterial = (RelativeLayout) findViewById(R.id.rlMaterialUsage);
    txtBarcode = (TextView) findViewById(R.id.txtBarcode);
    txtLocation = (TextView) findViewById(R.id.txtLocation);
    txtBuilding = (TextView) findViewById(R.id.txtBuilding);
    txtFloor = (TextView) findViewById(R.id.txtFloor);
    txtTrapType = (TextView) findViewById(R.id.txtTrapType);
    txtTrapNumber = (TextView) findViewById(R.id.txtNumber);
    txtMessage = (TextView) findViewById(R.id.txtMessage);
    imgClean = (ImageView) findViewById(R.id.imgClean);
    tgbRemoved = (ToggleButton) findViewById(R.id.tgbremoved);
    spnBaitCondition = (Spinner) findViewById(R.id.spnbait_condition_id);
    spnTrapCondition = (Spinner) findViewById(R.id.spntrap_condition_id);
    edtException = (EditText) findViewById(R.id.edtexception);
    txtExceptionCount = (TextView) findViewById(R.id.txtCount);
    btnSaveTrapData.setOnClickListener(this);
    btnClean.setOnClickListener(this);
    rlCaptured.setOnClickListener(this);
    rlEvidence.setOnClickListener(this);
    rlMaterial.setOnClickListener(this);
    trapscan_info = TrapList.Instance().getTrapByBarcodeNdCustomerId(barcode, cust_id);
    inspection = InspectionList.Instance().getInspectionByApp_Id(appointment_id, barcode);
    if (inspection == null) {
      isEdit = false;
      try {
        inspection = FieldworkApplication.Connection().newEntity(InspectionInfo.class);
        inspection.id = -1;
        inspection.barcode = barcode;
        inspection.save();
      } catch (ActiveRecordException e) {
        e.printStackTrace();
      }
    } else {
      isEdit = true;
      m_inspectionPests =
          InspectionPestsList.Instance().getInspectionPestByInspectionId(inspection.id);
      isClean = getIsClean(inspection, m_inspectionPests);
      isCleanInspection = getIsClean(inspection, m_inspectionPests);
      if (isClean) {
        imgClean.setVisibility(View.VISIBLE);
      }
    }
    LoadValues();
    LoadSpinners();
    if (isEdit) {
      loadMaterial();
    }
    tgbRemoved.setOnCheckedChangeListener(
        new OnCheckedChangeListener() {

          @Override
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            isRemoved = isChecked;
          }
        });
  };