Example #1
0
  public void iterateBlocks(String dateString) {
    blockList.clear();
    // Iterate through the blocks
    bList = MainActivity.db.getBlocksBetweenDate(start, stop);
    blockStatesList.clear();
    hoursDay = 0;
    minutesDay = 0;

    Iterator<Block> it = bList.iterator();
    while (it.hasNext()) {

      b = it.next();
      if (b.getStop() != 0) {
        s = "";
        if (b.getChecked() == 1) {
          s += Html.fromHtml((String) "&#10003;").toString();
          s += " ";
        }

        s += new SimpleDateFormat("HH:mm").format(b.getStart());
        s += " - ";
        s += new SimpleDateFormat("HH:mm").format(b.getStop());
        if (b.getOrderID() != 0) {
          Order order = MainActivity.db.getOrder(b.getOrderID());
          s += " - " + order.getOrderName();
        }

        s += " - " + b.printTime();

        // adds the hours and minutes of a block to hoursDay and minutesDay

        if (b.getChecked() == 1) {
          if (b.getStop() != 0) timeDiff = b.getStop() - b.getStart();
          else timeDiff = System.currentTimeMillis() - b.getStart();

          timeDiff -= 1000 * 60 * 60;

          Date date = new java.util.Date(timeDiff);

          hoursDay += date.getHours();
          minutesDay += date.getMinutes();
          if (minutesDay > 60) {
            minutesDay = minutesDay % 60;
            hoursDay++;
          }
        }

        if (b.toDateString().equals(dateString)) {
          blockList.add(s);

          if (!(b.getOrderID() <= 1)) {
            blockStatesList.add(b.getChecked());
          } else {
            blockStatesList.add(2);
          }
        }
      }
    }

    listAdapter =
        new CustomListAdapter1(
            this, R.layout.listrow, blockList, blockStatesList, "neosanslight.ttf");
    l_view.setAdapter(listAdapter);
    l_view.setOnItemClickListener(
        new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> adapter, View v, final int position, long arg3) {
            String value = (String) adapter.getItemAtPosition(position);
            try {
              if (!(bList.get(position).getOrderID() <= 1)) {
                bList.get(position).setChecked(switchChecked(bList.get(position).getChecked()));
                blockStatesList.set(position, bList.get(position).getChecked());
                listAdapter.setBlockStatesList(blockStatesList);
                Log.e(
                    "Changed item checked to: ",
                    Integer.toString(bList.get(position).getChecked()));
                MainActivity.db.putBlock(bList.get(position));
              } else {
                AlertDialog.Builder noOrderNrDialogBuilder =
                    new AlertDialog.Builder(l_view.getContext());
                noOrderNrDialogBuilder.setMessage(
                    "Denna post saknar ordernummer. \nVill du åtgärda detta?");
                noOrderNrDialogBuilder.setCancelable(true);
                noOrderNrDialogBuilder.setPositiveButton(
                    "Ja",
                    new DialogInterface.OnClickListener() {

                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        Block block = bList.get(position);
                        Intent i = new Intent(getApplicationContext(), NewOrderActivity.class);

                        i.putExtra("Block", block);
                        i.putExtra("Caller", "Checkview");

                        startActivity(i);
                      }
                    });

                noOrderNrDialogBuilder.setNegativeButton(
                    "Nej",
                    new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                      }
                    });
                AlertDialog alertNoOrder = noOrderNrDialogBuilder.create();
                alertNoOrder.show();
              }
            } catch (Exception e) {
              Log.e("NÃ¥got blev fel! At position:", Integer.toString(position));
            }
            Log.e("Värde i listan: ", value);
            Log.e("Position i listan: ", Integer.toString(position));
            iterateBlocks(getDateString());
          }
        });

    // Send the user to NewOrderActivity on longclick.
    l_view.setLongClickable(true);
    l_view.setOnItemLongClickListener(
        new OnItemLongClickListener() {

          @Override
          public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) {
            Block block = bList.get(pos);
            Intent i = new Intent(getApplicationContext(), NewOrderActivity.class);

            i.putExtra("Block", block);
            i.putExtra("Caller", "Checkview");

            startActivity(i);
            return false;
          }
        });

    day.setText(dateString);
    s = " Sammanlagd tid: " + hoursDay + "h " + minutesDay + "m";
    total.setText(s);
  }