0
hello, I’m doing a work ... of an android app in 3 layers ... and it’s a review app for bars and snacks ... which consists of taking the name of the place ... the address .. the email and the evaluation ... however I am with a difficulty ... I want that when picking up the location it has to compare if the typed location is already existing to the database and not let it enter again.
but when I try to do that ... my method is not right ... it closes or inserts normally ... the method is this
This is the libclass
public class Local {
private int id;
private String local;
private String endereco;
private String email;
private String telefone;
private Float avaliacao;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLocal() {
return local;
}
public void setLocal(String local) {
this.local = local;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public Float getAvaliacao() {
return avaliacao;
}
public void setAvaliacao(Float avaliacao) {
this.avaliacao = avaliacao;
}
}
This is the method of the button that adds:
public void adicionar(View v) {
if (editLocal.getText().toString().trim().isEmpty()) {
editLocal.setError("Coloque um Local!");
} else {
if (editEndereco.getText().toString().trim().isEmpty()) {
editEndereco.setError("Coloque um Endereço");
} else {
if (editTelefone.getText().toString().isEmpty()) {
editTelefone.setError("Coloque um Telefone!");
} else {
Local local = new Local();
local.setId(0);
local.setLocal(editLocal.getText().toString());
local.setEndereco(editEndereco.getText().toString());
local.setEmail(editEmail.getText().toString());
local.setTelefone(editTelefone.getText().toString());
local.setAvaliacao(ratingBarNota.getRating());
LocalBLL localBLL = new LocalBLL(getApplicationContext());
Utilidades util = new Utilidades();
//aqui começa a comparacao
nomeLocal = editLocal.getText().toString();
compara = localBLL.comparaLocais(nomeLocal);
if (compara == 1) {
util.exibirToast(getApplicationContext(), "Local já Cadastrado!");
} else {
try {
localBLL.insertLocal(local);
util.exibirToast(getApplicationContext(), "Você Salvou um Local!");
popularListView();
cancelar();
} catch(Exception ex) {
util.exibirToast(getApplicationContext(), getExternalCacheDir().toString());
}
}
}
}
}
}
And that’s the way of libBLL:
public int comparaLocais(String nomeLocal) {
LocalDAL localDAL = new LocalDAL(context);
Local local = null;
return localDAL.comparaLocal(nomeLocal);
}
And that’s the way of libDAL:
public int comparaLocal(String nomeLocal) {
String SELECT_LOCAISCOMPARACAO = "SELECT * from locais where nome_local ='" + nomeLocal + "'";
BancoDados banco = new BancoDados(context);
SQLiteDatabase db = banco.getReadableDatabase();
Cursor cursor = db.rawQuery(SELECT_LOCAISCOMPARACAO, null);
Local local = null;
if (cursor.moveToFirst()) {
local = new Local();
do {
local.setId(cursor.getInt(0));
local.setLocal(cursor.getString(1));
local.setEndereco(cursor.getString(2));
local.setEmail(cursor.getString(3));
local.setTelefone(cursor.getString(4));
local.setAvaliacao(cursor.getFloat(5));
} while ( cursor . moveToNext ());
}
comparacao = local.getLocal();
if (nomeLocal == comparacao) {
return 1;
} else {
return 0;
}
}
In my view ... it was supposed to be working ... I’m beginner in Android ... so if you can help me thank you very much ... I’m breaking my head kk to see the variable compare does not get the value of local.getlocal();
I’d appreciate it if someone knew right away The bug in logcat is this from androidstudio is this:
Hello ... I appreciate the help however, the error persists ... I did as you spoke loudenvier ... and as Voce said Thiago .. but the error is the same ... it seems to me that the variable compares not taking the value of local.getlocal .... I used the equalsingnorecase and it stops when it arrives in this part ... and the equals normal tbm ... was thus the code
– Filipe
if(cursor.moveToFirst()) { local = new Local(); do { local.setId(cursor.getInt(0)); local.setLocal(cursor.getString(1)); local.setEndereco(cursor.getString(2); local.setEmail(cursor.getString(3));
 local.setTelefone(cursor.getString(4));
 local.setAvaliacao(cursor.getFloat(5));

 } while(cursor.moveToNext());

 }
 comparacao = local.getLocal();
 if (nameLocal.equalsIgnoreCase(comparison)) { Return 1; }Else { Return 0; }
– Filipe
puts ... was left without indentation the code ... for the apology...
– Filipe
the code did not fit here ... but anyway .. only was placed the ignore case and the error persists ... if you can help thank you
– Filipe
@Loudenvier exists but some command I take the information and put inside the variable to compare ? ... I guess it’s not working out that way
– Filipe
I understood what you said ... just do not know how to put here ... can give an example of how it would be right ?
– Filipe
@Filipe see my answer, I edited it and added a more complete answer of how your code should look, adapt it in your program and see resolve.
– Thiago Queiroz
thank you so much for the help ... as I said I’m starting to develop ... and my mistake was from logica msm ... I speak only that Lse at the end there to work ... thank you so much for your help ... vlw msm
– Filipe
@Loudenvier thanks to you too ... helped me a lot! vlw msm
– Filipe