Exemplo n.º 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dtp);

    for (Model row : Drivers.getObjects()) {
      drivers.add(new MyItem(row.getId(), ((Drivers) row).getFio()));
    }

    for (Model row : Gaishnik.getObjects()) {
      gaishniks.add(new MyItem(row.getId(), ((Gaishnik) row).getFio()));
    }

    for (Model row : Pdd.getObjects()) {
      pdds.add(
          new MyItem(row.getId(), ((Pdd) row).getNumber() + "/" + ((Pdd) row).getDescription()));
    }

    dtps = Dtp.getObjects();

    addButton = (Button) findViewById(R.id.button_add);
    addButton.setOnClickListener(
        new OnClickListener() {

          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            final Dtp as = new Dtp();
            final Dialog di = new Dialog(DtpActivity.this);
            di.setTitle("Добавить запись");
            di.setContentView(R.layout.dialog_dtp);

            Button dateButton = (Button) di.findViewById(R.id.buttonDate);

            OnDateSetListener myCallBack =
                new OnDateSetListener() {

                  public void onDateSet(
                      DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    myYear = year;
                    myMonth = monthOfYear;
                    myDay = dayOfMonth;
                  }
                };

            final DatePickerDialog pick =
                new DatePickerDialog(DtpActivity.this, myCallBack, myYear, myMonth, myDay);
            pick.setTitle("Выберите дату");
            dateButton.setOnClickListener(
                new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                    pick.show();
                  }
                });

            final Spinner spinnerDriver = (Spinner) di.findViewById(R.id.spinner1);
            final Spinner spinnerGaishnik = (Spinner) di.findViewById(R.id.spinner2);
            final Spinner spinnerPdd = (Spinner) di.findViewById(R.id.spinner3);

            spinnerGaishnik.setAdapter(
                new ArrayAdapter<MyItem>(
                    DtpActivity.this, android.R.layout.simple_spinner_item, gaishniks));
            spinnerDriver.setAdapter(
                new ArrayAdapter<MyItem>(
                    DtpActivity.this, android.R.layout.simple_spinner_item, drivers));
            spinnerPdd.setAdapter(
                new ArrayAdapter<MyItem>(
                    DtpActivity.this, android.R.layout.simple_spinner_item, pdds));

            spinnerDriver.setOnItemSelectedListener(
                new OnItemSelectedListener() {

                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
                    tempDriver = (Drivers) Drivers.getObjectById(drivers.get(position).getId());
                  }

                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {}
                });

            spinnerGaishnik.setOnItemSelectedListener(
                new OnItemSelectedListener() {

                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
                    tempGaishnik =
                        (Gaishnik) Gaishnik.getObjectById(gaishniks.get(position).getId());
                  }

                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {}
                });

            spinnerPdd.setOnItemSelectedListener(
                new OnItemSelectedListener() {

                  @Override
                  public void onItemSelected(AdapterView<?> arg0, View v, int position, long id) {
                    tempPdd = (Pdd) Pdd.getObjectById(pdds.get(position).getId());
                  }

                  @Override
                  public void onNothingSelected(AdapterView<?> arg0) {}
                });

            Button saveButton = (Button) di.findViewById(R.id.button_save);
            saveButton.setOnClickListener(
                new OnClickListener() {

                  @Override
                  public void onClick(View v) {

                    as.setDriver(tempDriver);
                    as.setPdd(tempPdd);
                    as.setGaishnik(tempGaishnik);
                    as.setDate(myYear + "-" + myMonth + "-" + myDay);
                    dtps.add(as);
                    new DtpLoader(TABLE_INSERT, as).execute();
                    di.cancel();
                  }
                });
            di.show();
          }
        });

    listView = (ListView) findViewById(R.id.listView);

    listView.setOnItemClickListener(
        new OnItemClickListener() {

          @Override
          public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
            final Dtp as = (Dtp) dtps.get(position);
            final Dialog di = new Dialog(DtpActivity.this);
            di.setTitle("Просмотр");
            di.setContentView(R.layout.dialog_dtp_show);

            final TextView e1 = (TextView) di.findViewById(R.id.title_11);
            final TextView e2 = (TextView) di.findViewById(R.id.title_22);
            final TextView e3 = (TextView) di.findViewById(R.id.title_33);
            final TextView e4 = (TextView) di.findViewById(R.id.title_44);

            e1.setText(as.getDriver().getFio());
            e2.setText(as.getGaishnik().getFio());
            e3.setText(as.getPdd().getNumber() + "/" + as.getPdd().getDescription());
            e4.setText(as.getDate());
            Button saveButton = (Button) di.findViewById(R.id.button_close);
            saveButton.setOnClickListener(
                new OnClickListener() {

                  @Override
                  public void onClick(View v) {
                    di.cancel();
                  }
                });

            di.show();
          }
        });

    listView.setOnItemLongClickListener(
        new OnItemLongClickListener() {

          @Override
          public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long arg3) {
            final CharSequence[] items = {"Изменить", "Удалить"};
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setTitle("Выберите действие");
            final Dtp as = (Dtp) dtps.get(position);
            builder.setItems(
                items,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                    if (item == 0) {

                      final Dialog di = new Dialog(DtpActivity.this);

                      di.setTitle("Изменить");
                      di.setContentView(R.layout.dialog_dtp);
                      Button dateButton = (Button) di.findViewById(R.id.buttonDate);
                      String date = as.getDate();
                      String[] tokens = date.split("-");
                      myYear = Integer.valueOf(tokens[0]);
                      myMonth = Integer.valueOf(tokens[1]);
                      myDay = Integer.valueOf(tokens[2]);

                      OnDateSetListener myCallBack =
                          new OnDateSetListener() {

                            public void onDateSet(
                                DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                              myYear = year;
                              myMonth = monthOfYear;
                              myDay = dayOfMonth;
                            }
                          };

                      final DatePickerDialog pick =
                          new DatePickerDialog(
                              DtpActivity.this, myCallBack, myYear, myMonth, myDay);
                      pick.setTitle("Выберите дату");
                      dateButton.setOnClickListener(
                          new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                              pick.show();
                            }
                          });

                      final Spinner spinnerDriver = (Spinner) di.findViewById(R.id.spinner1);
                      final Spinner spinnerGaishnik = (Spinner) di.findViewById(R.id.spinner2);
                      final Spinner spinnerPdd = (Spinner) di.findViewById(R.id.spinner3);

                      spinnerDriver.setAdapter(
                          new ArrayAdapter<MyItem>(
                              DtpActivity.this, android.R.layout.simple_spinner_item, drivers));
                      spinnerGaishnik.setAdapter(
                          new ArrayAdapter<MyItem>(
                              DtpActivity.this, android.R.layout.simple_spinner_item, gaishniks));
                      spinnerPdd.setAdapter(
                          new ArrayAdapter<MyItem>(
                              DtpActivity.this, android.R.layout.simple_spinner_item, pdds));

                      spinnerDriver.setOnItemSelectedListener(
                          new OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(
                                AdapterView<?> arg0, View v, int position, long id) {
                              tempDriver =
                                  (Drivers) Drivers.getObjectById(drivers.get(position).getId());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {}
                          });

                      int i = 0;
                      for (MyItem row : drivers) {
                        if (row.getId() == as.getDriver().getId()) spinnerDriver.setSelection(i);
                        i++;
                      }

                      spinnerGaishnik.setOnItemSelectedListener(
                          new OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(
                                AdapterView<?> arg0, View v, int position, long id) {
                              tempGaishnik =
                                  (Gaishnik)
                                      Gaishnik.getObjectById(gaishniks.get(position).getId());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {}
                          });

                      i = 0;
                      for (MyItem row : gaishniks) {
                        if (row.getId() == as.getGaishnik().getId())
                          spinnerGaishnik.setSelection(i);
                        i++;
                      }

                      spinnerPdd.setOnItemSelectedListener(
                          new OnItemSelectedListener() {

                            @Override
                            public void onItemSelected(
                                AdapterView<?> arg0, View v, int position, long id) {
                              tempPdd = (Pdd) Pdd.getObjectById(pdds.get(position).getId());
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> arg0) {}
                          });

                      i = 0;
                      for (MyItem row : pdds) {
                        if (row.getId() == as.getPdd().getId()) spinnerPdd.setSelection(i);
                        i++;
                      }

                      Button saveButton = (Button) di.findViewById(R.id.button_save);
                      saveButton.setOnClickListener(
                          new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                              as.setDriver(tempDriver);
                              as.setGaishnik(tempGaishnik);
                              as.setPdd(tempPdd);
                              as.setDate(myYear + "-" + myMonth + "-" + myDay);

                              new DtpLoader(TABLE_UPDATE, as).execute();
                              di.cancel();
                              listView.setAdapter(new DtpAdapter());
                            }
                          });

                      di.show();
                    } else {

                      AlertDialog.Builder ad = new AlertDialog.Builder(DtpActivity.this);
                      ad.setMessage("Вы уверены?");
                      ad.setPositiveButton(
                          "Да",
                          new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              new DtpLoader(TABLE_DELETE, as).execute();
                              listView.setAdapter(new DtpAdapter());
                              dtps.remove(as);
                            }
                          });
                      ad.setNegativeButton(
                          "Нет",
                          new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                              dialog.cancel();
                            }
                          });

                      ad.show();
                    }
                  }
                });

            builder.setNegativeButton(
                "Закрыть",
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                  }
                });
            AlertDialog alert = builder.create();
            alert.show();

            return false;
          }
        });
    listView.setAdapter(new DtpAdapter());
  }
Exemplo n.º 2
0
    @Override
    protected Integer doInBackground(Void... params) {
      String sql = null;
      ResultSet reset = null;
      try {
        Statement st = ConnectionHelper.stmt;

        switch (tableState) {
          case TABLE_DELETE:
            sql = "DELETE FROM Offence WHERE id = " + object.getId();
            Log.v("Registration", sql);
            st.execute(sql);
            break;

          case TABLE_INSERT:
            sql =
                "INSERT INTO Offence VALUES("
                    + object.getDriver().getId()
                    + ","
                    + object.getGaishnik().getId()
                    + ","
                    + object.getPdd().getId()
                    + ",'"
                    + object.getDate()
                    + "'); SELECT SCOPE_IDENTITY()";
            Log.v("drivers", sql);
            reset = st.executeQuery(sql);

            while (reset.next()) {
              object.setId(reset.getInt(1));
              Log.w("Data:", "drivers");
            }

            break;

          case TABLE_UPDATE:
            sql =
                "UPDATE Offence "
                    + "SET driver_id = "
                    + object.getDriver().getId()
                    + ", gai_person_id = "
                    + object.getGaishnik().getId()
                    + ", pdd_id = "
                    + object.getPdd().getId()
                    + ", date='"
                    + object.getDate()
                    + "' WHERE id = "
                    + object.getId();
            Log.v("Registration", sql);
            st.executeUpdate(sql);

            break;

          default:
            break;
        }

      } catch (SQLException e) {
        e.printStackTrace();
        return Integer.valueOf(2);
      }

      return null;
    }