Good night,
I'm trying to make a small app that consists of searching a SQLite database. For the research I will use two forms, that is through the UN Numero and through the Name.
However, I'm encountering a problem, the app pops up in case you try to search for a record that does not exist.
How can I trigger an error message if the search does not find a result?
DbHelper:
//Metodo ONU Query
public List<Elemento> getONU()
{
List<Elemento> listaElementos = new ArrayList<Elemento>();
//Seleccionar Query
String selectQuery = "SELECT *FROM " + TABLE_NAME + " WHERE ONU = " + MainActivity.editText_ONU_search.getText();
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
//Loop por todas as linhas da BD e adiciona-las a lista
if(cursor.moveToFirst())
{
Elemento element = new Elemento();
element.setID(cursor.getInt(0));
element.setONU(cursor.getInt(1));
element.setNOME_SUB(cursor.getString(2));
listaElementos.add(element);
}
//Return listaElementos;
return listaElementos;
}
//Metodo Nome Query
public List<Elemento> getNomeSubstancia()
{
List<Elemento> listaElementos = new ArrayList<Elemento>();
//Seleccionar Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE NOME_SUB = '" + MainActivity.editText_NOME_search.getText() + "'";
dbase = this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
//Loop por todas as linhas da BD e adiciona-las a lista
if(cursor.moveToFirst())
{
Elemento element = new Elemento();
element.setID(cursor.getInt(0));
element.setONU(cursor.getInt(1));
element.setNOME_SUB(cursor.getString(2));
listaElementos.add(element);
}
//Return listaElementos;
return listaElementos;
}
OnuResultActivity:
public class OnuResultActivity extends AppCompatActivity {
//Declaração de variaveis
List<Elemento> listaElementos;
int elemento_id = 0;
Elemento currentElemento;
TextView textView_onu, textView_nome_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onu_result);
DbHelper db = new DbHelper(this);
listaElementos = db.getONU();
currentElemento = listaElementos.get(elemento_id);
textView_onu = (TextView)findViewById(R.id.textView_onu);
textView_nome_sub = (TextView) findViewById(R.id.textView_nome_sub);
//Apresentar Info
setElementosView();
}
private void setElementosView() {
textView_onu.setText(Integer.toString(currentElemento.getONU()));
textView_nome_sub.setText(String.valueOf(currentElemento.getNOME_SUB()));
elemento_id++;
}
}
CreativeActivityName:
public class NomeResultActivity extends AppCompatActivity {
//Declaração de variaveis
List<Elemento> listaElementos;
int elemento_id = 0;
Elemento currentElemento;
TextView textView_onu, textView_nome_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nome_result);
DbHelper db = new DbHelper(this);
listaElementos = db.getNomeSubstancia();
currentElemento = listaElementos.get(elemento_id);
textView_onu = (TextView)findViewById(R.id.textView_onu);
textView_nome_sub = (TextView) findViewById(R.id.textView_nome_sub);
//Apresentar Info
setElementosView();
}
private void setElementosView() {
textView_onu.setText(Integer.toString(currentElemento.getONU()));
textView_nome_sub.setText(String.valueOf(currentElemento.getNOME_SUB()));
elemento_id++;
}
}
Any idea how I can return a warning message when the search is not found to cover the possibility that the search entered by the user does not match what is in the database.
Greetings