0
Good night,
I’m trying to make a little app that consists of searching a Sqlite database. For the research I will use two forms, ie through the UN Number and through the Name.
However I am facing a problem, the app bursts in case you try to search for any registration that does not exist.
How can I trigger an error message in case 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++;
}
}
Nomeresultactivity:
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 in order to cover the possibility that the search entered by the user is not matching what is in the BD.
Regards