Check if Registration Exists in BD Sqlite and if there is no Return Message

Asked

Viewed 792 times

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

2 answers

0

The way I do this type of task and the next, right after the query I make a comparison to see if the query returned any result, then I take my Cursor and check it, if(cursor.getCount() == 0){ message }So if you don’t have any records in the bank you’ll return 0 lines, out of curiosity where you’re from? good luck in school

0

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onu_result);

DbHelper db = new DbHelper(this);
listaElementos = db.getONU();

if(listaElementos.size() > 0){
   currentElemento = listaElementos.get(elemento_id);
}else{
  //AQUI VOCÊ PODE USAR UM TOAST OU UM DIALGO PRA EXIBIR A 
  //MENSAGEM QUE QUISER
}
textView_onu = (TextView)findViewById(R.id.textView_onu);
textView_nome_sub = (TextView) findViewById(R.id.textView_nome_sub);

//Apresentar Info
setElementosView();

}

I guess that’s what I understood from your question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.