Check if Registry exists in DB SQLite and if there is no Return Message

0

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

    
asked by anonymous 27.11.2016 / 23:58

1 answer

0

The way I do this type of task and the following, right after the query I make a comparison to see if the query returned some result, then I take my Cursor and check it, if (cursor.getCount () = = 0) {message}, so if you do not have records in the database will return 0 lines, out of curiosity where are you from? good luck in studies

    
28.11.2016 / 01:23