@Override
  public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
      LayoutInflater inflater =
          (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.lessons_list, null);
    }

    if (mSelectedPos == position) {
      convertView.setBackgroundColor(
          mActivity.getResources().getColor(android.R.color.holo_blue_dark));
    } else {
      convertView.setBackgroundColor(
          mActivity.getResources().getColor(android.R.color.transparent));
    }

    TextView itemNameView = (TextView) convertView.findViewById(R.id.itemName);
    TextView itemDetailView = (TextView) convertView.findViewById(R.id.itemDetail);

    Lesson l = (Lesson) getItem(position);

    if (l != null) {
      itemNameView.setText(Html.fromHtml("" + l.getNumber() + ". " + l.getFormText()));
      itemDetailView.setText(Html.fromHtml(mFormat.format(l.getStart()) + l.getTeacher()));
    }

    return convertView;
  }
  public static void getLessonsDetails(Document doc, Schedule s) throws ParseException {

    final SimpleDateFormat fmt = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH);
    fmt.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));

    Elements tableCells = doc.getElementsByAttributeValue("class", "table diary");
    for (Element tableCell : tableCells) {

      int tdCount = 0;
      Date date = null;
      Lesson l, lPrev = null; // lPrev to handle duplicate lesson
      int sameLesson = 0; // Also to handle duplicate lesson

      Elements trs = tableCell.getElementsByTag("tr");
      for (Element tr : trs) {

        if (tr.hasAttr("class") && tr.attr("class").equals("table-header")) continue;

        l = null;
        sameLesson = 0; // assume no bug here
        Elements tds = tr.getElementsByTag("td");

        for (Element td : tds) {

          if (td.hasAttr("class") && td.attr("class").equals("date")) {

            date = fmt.parse(td.getElementsByTag("div").first().text());
            tdCount = 1;

          } else if (td.hasAttr("class") && td.attr("class").equals("diary-mark")) {

            String marks = fetchLongCellStringNoWhitespaces(td);
            if (l != null && marks != null) {

              if (sameLesson > 0 && lPrev != null) {

                l.setMarks(fixDuplicateString(marks, lPrev.getMarks(), sameLesson));
              } else l.setMarks(marks);
            }
            tdCount++;

          } else if (td.hasAttr("class") && td.attr("class").equals("diary-comment")) {

            String comment = fetchLongCellStringNoWhitespaces(td);
            if (l != null && comment != null) {

              if (sameLesson > 0 && lPrev != null) {

                l.setComment(fixDuplicateString(comment, lPrev.getComment(), sameLesson));
              } else l.setComment(comment);
            }

            tdCount++;

          } else if (tdCount == 2) {

            String theme = fetchLongCellStringNoWhitespaces(td);
            if (l != null && theme != null) {

              if (sameLesson > 0 && lPrev != null) {

                l.setTheme(fixDuplicateString(theme, lPrev.getTheme(), sameLesson));
              } else l.setTheme(theme);
            }
            tdCount++;

          } else if (tdCount == 3) {

            String homework = fetchLongCellStringNoWhitespaces(td);
            if (l != null && homework != null) {

              if (sameLesson > 0 && lPrev != null) {

                l.setHomework(fixDuplicateString(homework, lPrev.getHomework(), sameLesson));
              } else l.setHomework(homework);
            }
            tdCount++;

          } else if (SUBJECT_NAME.matcher(td.text()).find()) {

            tdCount = 2;
            int number = Integer.parseInt(td.text().substring(0, 1));
            l = s.getLessonByNumber(date, number);

            if (lPrev != null
                && l != null
                && l.getStart().equals(lPrev.getStart())
                && l.getNumber() == lPrev.getNumber()) {

              // We hit the same lesson bug
              sameLesson++;
            }

          } else {
            tdCount++;
          }
        }

        if (l != null) {
          lPrev = l;
          l.update();
        }
      }
    }
  }
  public static void getLessons(Document doc, Schedule s) throws ParseException {

    final SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH);
    format.setTimeZone(TimeZone.getTimeZone("Europe/Moscow"));

    Elements lessonCells = doc.getElementsByAttribute("number");

    for (Element lessonCell : lessonCells) {

      Lesson l, lPrev = null; // lPrev to handle duplicate lesson
      int sameLesson = 0; // Also to handle duplicate lesson

      int number = Integer.parseInt(lessonCell.attr("number"));
      String time = "";

      Elements timeDetails = lessonCell.getElementsByClass("cell-header2");
      for (Element timeDetail : timeDetails) {
        if (timeDetail.hasAttr("style")) time = timeDetail.text();
      }

      Elements lessonCellDetails = lessonCell.getElementsByAttribute("jsdate");
      for (Element lessonCellDetail : lessonCellDetails) {

        String date = lessonCellDetail.attr("jsdate");
        int index = 0;
        sameLesson = 0;

        for (Element subject :
            lessonCellDetail.getElementsByAttributeValue("class", "lesson-subject")) {

          if (subject == null || subject.text() == null || subject.text().length() <= 0) {
            // No lesson scheduled
            continue;
          }

          Date start = format.parse(date + " " + time.substring(0, time.indexOf("-") - 1));
          if ((l = s.getLessonByNumber(start, number)) == null) {

            if (BuildConfig.DEBUG)
              Log.d("GshisHTMLParser", TS.get() + " getLessons() not found in db, will insert");

            l = new Lesson();
            sameLesson = 0;

            l.setStart(start);
            l.setStop(
                format.parse(date + " " + time.substring(time.indexOf("-") + 2, time.length())));
            l.setFormId(subject.attr("id"));
            l.setFormText(subject.text());
            l.setTeacher(
                lessonCellDetail
                    .getElementsByAttributeValue("class", "lesson-teacher")
                    .get(sameLesson)
                    .text());
            l.setNumber(number);

            s.addLesson(l);

          } else {

            if (BuildConfig.DEBUG)
              Log.d("GshisHTMLParser", TS.get() + " getLessons() found in db, will update");

            l.setFormId(subject.attr("id"));

            if (lPrev != null && lPrev.getStart().equals(start) && lPrev.getNumber() == number) {

              if (BuildConfig.DEBUG)
                Log.d(
                    "GshisHTMLParser",
                    TS.get()
                        + " getLessons() dup = "
                        + subject.text()
                        + " index = "
                        + index
                        + " sameLesson = "
                        + sameLesson);

              sameLesson++;

              if (!lPrev.getFormText().equals(subject.text()))
                l.setFormText(fixDuplicateString(subject.text(), lPrev.getFormText(), sameLesson));

              String teacher =
                  lessonCellDetail
                      .getElementsByAttributeValue("class", "lesson-teacher")
                      .get(index)
                      .text();

              if (!lPrev.getTeacher().equals(teacher))
                l.setTeacher(fixDuplicateString(teacher, lPrev.getTeacher(), sameLesson));

            } else {

              l.setNumber(number);
              l.setFormText(subject.text());
              l.setTeacher(
                  lessonCellDetail
                      .getElementsByAttributeValue("class", "lesson-teacher")
                      .get(index)
                      .text());
            }

            l.update();
          }

          lPrev = l;
          index++;
        }
      }
    }
  }