Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RestauranteCadastrar extends AppCompatActivity {
- EditText edtNome, edtEndereco, edtDescricao;
- Button btnConfirmar;
- RestauranteDAO dao = new RestauranteDAO(RestauranteCadastrar.this);
- TextView txtListagem;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_restaurante_cadastrar);
- getSupportActionBar().setDisplayHomeAsUpEnabled(true); //Mostrar o botão
- getSupportActionBar().setHomeButtonEnabled(true); //Ativar o botão
- edtNome = (EditText) findViewById(R.id.edtNome);
- edtEndereco = (EditText) findViewById(R.id.edtEndereco);
- edtDescricao = (EditText) findViewById(R.id.edtDescricao);
- btnConfirmar = (Button) findViewById(R.id.btnConfirmar);
- CarregarInicial();
- btnConfirmar.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- boolean camposPreenchidos = true;
- if (edtNome.getText().toString().isEmpty()){
- edtNome.setError("Campo Obrigatório.");
- edtNome.requestFocus();
- camposPreenchidos = false;
- }
- if (edtEndereco.getText().toString().isEmpty()){
- edtEndereco.setError("Campo Obrigatório.");
- edtEndereco.requestFocus();
- camposPreenchidos = false;
- }
- if (edtDescricao.getText().toString().isEmpty()){
- edtDescricao.setError("Campo Obrigatório.");
- edtDescricao.requestFocus();
- camposPreenchidos = false;
- }
- if (camposPreenchidos){
- Restaurante restaurante = new Restaurante();
- restaurante.setNome(edtNome.getText().toString());
- restaurante.setDescricao(edtDescricao.getText().toString());
- restaurante.setEndereco(edtEndereco.getText().toString());
- restaurante.setLike(0);
- restaurante.setDeslike(0);
- restaurante.setLikeQuant(0);
- restaurante.setDeslikeQuant(0);
- dao.Abrir();
- dao.Inserir(restaurante);
- dao.Fechar();
- Toast msg = Toast.makeText(RestauranteCadastrar.this, "Recomendação cadastrada com sucesso!",Toast.LENGTH_LONG);
- msg.show();
- //finish();
- }
- }
- });
- }
- public void CarregarInicial(){
- RestauranteDAO dao = new RestauranteDAO(RestauranteCadastrar.this);
- dao.Abrir();
- Listar(dao);
- }
- public void Listar(RestauranteDAO dao){
- List<Restaurante> lista = dao.ListarTodos();
- txtListagem = (TextView) findViewById(R.id.txtListagem);
- txtListagem.setText("");
- for (Restaurante c: lista){ //for each
- txtListagem.setText(txtListagem.getText().toString() + c.getId() + "-" +c.getNome() + "n");
- }
- }
- public class RestauranteDAO {
- private SQLiteDatabase db;
- private DBHelper helper;
- public RestauranteDAO(Context context){
- helper = new DBHelper(context);
- }
- public void Abrir(){
- db = helper.getReadableDatabase();
- }
- public void Fechar(){
- helper.close();
- }
- public long Inserir(Restaurante restaurante){
- ContentValues dados = new ContentValues();
- String query = "select * from restaurante";
- Cursor cursor = db.rawQuery(query, null);
- int count = cursor.getCount();
- dados.put("ID", count);
- dados.put("Nome", restaurante.getNome());
- dados.put("Descricao", restaurante.getDescricao());
- dados.put("Endereco", restaurante.getEndereco());
- dados.put("Like", restaurante.getLike());
- dados.put("Deslike", restaurante.getDeslike());
- dados.put("LiqueQuant", restaurante.getLikeQuant());
- dados.put("DeslikeQuant", restaurante.getDeslikeQuant());
- long rowid = db.insert(DBHelper.TBL_RESTAURANTE, null, dados);
- return rowid;
- }
- public long Atualizar(int id, int like, int deslike){
- ContentValues dados = new ContentValues();
- String where = "ID = " + id;
- Abrir();
- dados.put("Like", like);
- dados.put("Deslike", deslike);
- long rowid = db.update(DBHelper.TBL_RESTAURANTE, dados, where, null);
- Fechar();
- return rowid;
- }
- public Cursor carregaDados(){
- Cursor cursor;
- Abrir();
- cursor = db.query(DBHelper.TBL_RESTAURANTE, new String[]{"ID", "Nome", "Descricao", "Endereco", "Like", "Deslike", "LikeQuant", "DeslikeQuant"}, null, null, null, null, "ID", null);
- if(cursor!=null) {
- cursor.moveToFirst();
- }
- Fechar();
- return cursor;
- }
- public ArrayList procurarID(int id){
- db = this.helper.getReadableDatabase();
- String query = "select * from " + DBHelper.TBL_RESTAURANTE;
- Cursor cursor = db.rawQuery(query, null);
- int idComparacao = 99;
- String vNome = "HQPZM", vDescricao = "HQPZM";
- int vLike = 0, vDeslike = 0;
- String vEndereco = "HQPZM";
- int vLikeQuant = 0, vDeslikeQuant = 0;
- if (cursor.moveToFirst()){
- do {
- idComparacao = cursor.getInt(0);
- if (idComparacao == id){
- vNome = cursor.getString(1);
- vDescricao = cursor.getString(2);
- vLike = cursor.getInt(3);
- vDeslike = cursor.getInt(4);
- vEndereco = cursor.getString(5);
- vLikeQuant = cursor.getInt(6);
- vDeslikeQuant = cursor.getInt(7);
- break;
- }
- } while (cursor.moveToNext());
- }
- db.close();
- ArrayList vetResult = new ArrayList();
- vetResult.add(idComparacao); //0
- vetResult.add(vNome); //1
- vetResult.add(vDescricao); //2
- vetResult.add(vLike); //3
- vetResult.add(vDeslike); //4
- vetResult.add(vEndereco); //5
- vetResult.add(vLikeQuant); //6
- vetResult.add(vDeslikeQuant);//7
- return(vetResult);
- }
- public List<Restaurante> ListarTodos(){
- List<Restaurante> lista = new ArrayList<Restaurante>();
- Cursor cursor = db.query(DBHelper.TBL_RESTAURANTE, new String[]{"ID", "Nome", "Endereco", "Descricao"}, null, null, null, null, "Nome");
- cursor.moveToFirst();
- while (!cursor.isAfterLast()){
- Restaurante restaurante = new Restaurante();
- restaurante.setId(cursor.getInt(0));
- restaurante.setNome(cursor.getString(1));
- restaurante.setDescricao(cursor.getString(2));
- lista.add(restaurante);
- cursor.moveToNext();
- }
- cursor.close();
- return lista;
- }
- }
Add Comment
Please, Sign In to add comment