· 9 years ago · Dec 06, 2016, 04:36 PM
1private SQLiteDatabase db;
2private banco banco;
3
4public ControlarAgedamento(Context context){banco = new banco(context);
5}
6
7public String insereAgenda(String nome, String dia, String horario, String corte, String valor){
8 ContentValues valores;
9 long resultado;
10
11 db = banco.getWritableDatabase();
12 valores = new ContentValues();
13 valores.put(banco.NOMECLIAGE, nome);
14 valores.put(banco.DATA, dia );
15 valores.put(banco.HORA, horario);
16 valores.put(banco.SERVICO,corte );
17 valores.put(banco.VALOR,valor );
18 validar(dia,horario);
19 resultado = db.insert(banco.TABELAAGE, null, valores);
20 db.close();
21
22 if (resultado ==-1)
23 return "Erro ao inserir registro";
24 else
25 return "Registro Inserido com sucesso";
26
27}
28
29public Cursor carregaDados(){
30 Cursor cursor;
31 String[] campos = {banco.IDAGE,banco.NOMECLIAGE,banco.DATA,banco.HORA,banco.SERVICO,banco.VALOR};
32 db = banco.getReadableDatabase();
33 cursor = db.query(banco.TABELAAGE, campos, null, null, null, null, null, null);
34
35 if(cursor!=null){
36 cursor.moveToFirst();
37 }
38 db.close();
39 return cursor;
40}
41
42public Cursor carregaDadoById(int id){
43 Cursor cursor;
44 String[] campos = {banco.IDAGE,banco.NOMECLIAGE,banco.DATA,banco.HORA,banco.SERVICO,banco.VALOR};
45 String where = banco.IDAGE + "=" + id;
46 db = banco.getReadableDatabase();
47 cursor = db.query(banco.TABELAAGE,campos,where, null, null, null, null, null);
48
49 if(cursor!=null){
50 cursor.moveToFirst();
51 }
52 db.close();
53 return cursor;
54}
55
56public void alteraHorario(int id, String nome, String telefone, boolean email, String idade, String valor){
57 ContentValues valores;
58 String where;
59
60 db = banco.getWritableDatabase();
61
62 where = banco.IDAGE + "=" + id;
63
64 valores = new ContentValues();
65 valores.put(banco.NOMECLIAGE, nome);
66 valores.put(banco.DATA, telefone);
67 valores.put(banco.HORA, email);
68 valores.put(banco.SERVICO, idade);
69 valores.put(banco.VALOR, valor);
70
71
72 db.update(banco.TABELAAGE,valores,where,null);
73 db.close();
74}
75public void deletaRegistro(int id){
76 String where = banco.IDAGE + "=" + id;
77 db = banco.getReadableDatabase();
78 db.delete(banco.TABELAAGE,where,null);
79 db.close();
80}
81
82public Integer validarData(final String DATA, final String HORA){
83
84 SQLiteDatabase db = banco.getWritableDatabase();
85 final Cursor cursor = db.query(banco.TABELAAGE, new String[]{"id"}, "DATA = ? AND HORA =?", new String[]{DATA,HORA}, null, null, null, null);
86 //se nulo ou vazio não encontrou nenhum com a mesma data e hora, retorna -1
87 if (null == cursor || !cursor.moveToFirst()) return -1;
88
89 final Integer id = cursor.getInt(0);
90 cursor.close();
91 return id;
92}
93
94public void validar(String dia, String horario) {
95 Integer idComDataEHora = validarData(dia, horario);
96 if (idComDataEHora.equals(-1)) {
97 // não exite nenhum registro
98 } else {
99 /// Se vc está editando, verifique se o id é igual ao que está editando!
100
101 }
102
103}
104
105public static final String TABELAPRO = "produto";
106public static final String IDPRO = "_id";
107public static final String IMGPRO = "imagem";
108public static final String NOMEPRO = "nome";
109public static final String QUANTIDADEPRO = "quantidade";
110public static final String MARCAPRO = "marca";
111//---------------------------------------------------------
112public static final String TABELAAGE = "agenda";
113public static final String IDAGE = "_id";
114public static final String NOMECLIAGE = "nome";
115public static final String DATA = "dia";
116public static final String HORA = "hora";
117public static final String SERVICO = "corte";
118public static final String VALOR = "valor";
119
120
121public banco(Context context){
122 super(context, NOME_BANCOCLI, null, VERSAOCLI);
123}
124
125
126@Override
127public void onCreate(SQLiteDatabase db) {
128 String sql = "CREATE TABLE "+TABELACLI+"("
129 +IDCLI+ " integer primary key autoincrement,"
130 +IMGCLI+ " blob,"
131 +NOMECLI+ " text not null,"
132 +TELEFONECLI+ " text not null,"
133 +CELULARCLI+ " text not null,"
134 +EMAILCLI+ " text not null,"
135 +SEXOCLI+ " text not null,"
136 +CODCLI+ " text not null"
137 +");";
138
139 db.execSQL(sql);
140
141 String sql2 = "CREATE TABLE "+TABELAFUN+"("
142 +IDFUN+ " integer primary key autoincrement,"
143 +IMGFUN+ " blob,"
144 +NOMEFUN+ " text not null,"
145 +TELEFONEFUN+ " text not null,"
146 +CELULARFUN+ " text not null,"
147 +SALARIOFUN+ " text not null,"
148 +CARGOFUN+ " text not null,"
149 +SEXOFUN+ " text not null"
150 +");";
151
152 db.execSQL(sql2);
153
154 String sql3 ="CREATE TABLE "+TABELAPRO+"("
155 +IDPRO+ " integer primary key autoincrement,"
156 +IMGPRO+ " blob,"
157 +NOMEPRO+ " text not null,"
158 +MARCAPRO+ " text,"
159 +QUANTIDADEPRO+ " text not null"
160 +");";
161
162 db.execSQL(sql3);
163
164 String sql4 ="CREATE TABLE "+TABELAAGE+"("
165 +IDAGE+ " integer primary key autoincrement,"
166 +NOMECLIAGE+ " text not null,"
167 +DATA+ " text not null,"
168 +HORA+ " text not null,"
169 +SERVICO+ " text not null,"
170 +VALOR+ " text not null"
171 +")";
172
173 db.execSQL(sql4);
174
175}
176
177@Override
178public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
179 db.execSQL("DROP TABLE IF EXISTS" + TABELACLI);
180 onCreate(db);
181 db.execSQL("DROP TABLE IF EXISTS" + TABELAAGE);
182 onCreate(db);
183 db.execSQL("DROP TABLE IF EXISTS" + TABELAFUN);
184 onCreate(db);
185 db.execSQL("DROP TABLE IF EXISTS" + TABELAPRO);
186 onCreate(db);
187}
188
189
190
191
192}
193
194private EditText Nome,Dia,Corte,valor;
195private AlertDialog alerta;
196private ImageButton data;
197private ImageView btncadastrar,btnvoltar;
198static final int DATE_DIALOG_ID = 0;
199private String DataFinal;
200private Spinner Hora;
201
202private banco banco;
203
204
205
206@Override
207protected void onCreate(Bundle savedInstanceState) {
208 super.onCreate(savedInstanceState);
209 setContentView(R.layout.cadastrar_horario);
210 data =(ImageButton) findViewById(R.id.imgdata);
211 data.setOnClickListener(this);
212
213
214 btncadastrar =(ImageView)findViewById(R.id.btnCadastrarHorarioSalvar);
215 Nome =(EditText) findViewById(R.id.edtNome);
216 Hora =(Spinner) findViewById(R.id.spinerhorario);
217 Dia = (EditText) findViewById(R.id.txtData);
218 Corte =(EditText) findViewById(R.id.edtCorte);
219 data=(ImageButton) findViewById(R.id.imgdata);
220 valor=(EditText) findViewById(R.id.edtValor);
221
222 ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinnerhorario, android.R.layout.simple_spinner_item);
223 Hora.setAdapter(adapter);
224
225 btncadastrar.setOnClickListener(new View.OnClickListener(){
226 @Override
227 public void onClick(View v) {
228 String spvalor = Hora.getSelectedItem().toString();
229 if(Dia.getText().length() == 0 ||Hora.equals(null) ||Corte.getText().length() == 0){
230 Toast.makeText(getApplicationContext(), "Nenhum campo pode estar vazio", Toast.LENGTH_LONG).show();
231 }else {
232
233
234 ControlarAgedamento ca = new ControlarAgedamento(getBaseContext());
235 String nomeString = Nome.getText().toString();
236 String diaString = Dia.getText().toString();
237 String horaString = spvalor;
238 String corteString = Corte.getText().toString();
239 String valorString = valor.getText().toString();
240
241 String resultado;
242
243 resultado = ca.insereAgenda(nomeString, diaString, horaString, corteString, valorString );
244
245 Intent i = new Intent(AgendarF.this, Agenda.class);
246 startActivity(i);
247 finish();
248 Toast.makeText(getApplicationContext(), "Horario Marcado", Toast.LENGTH_LONG).show();
249 }
250 }
251 });
252
253}
254@Override
255protected Dialog onCreateDialog(int id) {
256 Calendar calendario = Calendar.getInstance();
257
258 int ano = calendario.get(Calendar.YEAR);
259 int mes = calendario.get(Calendar.MONTH);
260 int dia = calendario.get(Calendar.DAY_OF_MONTH);
261
262 switch (id) {
263 case DATE_DIALOG_ID:
264 return new DatePickerDialog(this, mDateSetListener, ano, mes,
265 dia);
266 }
267 return null;
268}
269
270private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
271 public void onDateSet(DatePicker view, int year, int monthOfYear,
272 int dayOfMonth) {
273 String data = String.valueOf(dayOfMonth) + " /"
274 + String.valueOf(monthOfYear+1) + " /" + String.valueOf(year);
275 Toast.makeText(AgendarF.this,
276 "DATA = " + data, Toast.LENGTH_SHORT)
277 .show();
278 DataFinal= data;
279 Dia.setText(DataFinal);
280 }
281};
282
283@Override
284public void onClick(View v) {
285 if (v == data)
286 showDialog(DATE_DIALOG_ID);
287}
288//////////////Acaba Calendario//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
289////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
290
291
292
293
294}