Cursor is: android.database.sqlite.SQLiteCursor@428085b0

0

I'm trying to make a validation using the cursor I've equaling it to null but it never returns a null value, it always returns a value like this:

  

android.database.sqlite.SQLiteCursor@428085b0

I do not know what is due, is it an error? or how can I do a validation by matching my cursor to null.

This is the code I am using for my query:

Cursor cursor = sqLiteData.query("usuarios", new String[]{"nombre_usuario", "password", "passwordr"}, "nombre_usuario = ? ", new String[]{nombreusu}, null, null, null, null);

                                if(cursor != null){

                                    try {
                                        ContentValues values = new ContentValues();
                                        values.put("nombre_usuario", nombreusu);
                                        values.put("password", passr);
                                        values.put("passwordr", passrr);

                                        sqLiteDatabase.insert("usuarios", null, values);
                                        Toast.makeText(getApplication(), "El usuario se registro correctamente", Toast.LENGTH_SHORT).show();
                                        obtenerNombreUsuario();
                                    }catch (SQLException e){
                                        Toast.makeText(getApplication(), "El usuario ya existe", Toast.LENGTH_SHORT).show();
                                    }

                                }else{

                                    Toast.makeText(getApplicationContext(),"El usuario esta registrado"+cursor,Toast.LENGTH_LONG).show();
                                    cursor.moveToFirst();

                                }
    
asked by Kike hatake 03.04.2018 в 06:39
source

1 answer

0

Actually in this case cursor has no value of null, that's why your validation does not work correctly, in this case it is used:

while (cursor.moveToNext()) {
      ...
      ...
}

In this way, if the cursor contains values, each value in the loop is read.

Another way is to use the getCount () method, of course previously check if the cursor has no null value

 if(cursor != null && cursor.getCount() > 0){

You would apply it this way:

 if(cursor != null && cursor.getCount() > 0){

                                    Toast.makeText(getApplicationContext(),"El usuario esta registrado"+cursor,Toast.LENGTH_LONG).show();
                                    cursor.moveToFirst();

                                }else{


                                    try {
                                        ContentValues values = new ContentValues();
                                        values.put("nombre_usuario", nombreusu);
                                        values.put("password", passr);
                                        values.put("passwordr", passrr);

                                        sqLiteDatabase.insert("usuarios", null, values);
                                        Toast.makeText(getApplication(), "El usuario se registro correctamente", Toast.LENGTH_SHORT).show();
                                        obtenerNombreUsuario();
                                    }catch (SQLException e){
                                        Toast.makeText(getApplication(), "El usuario ya existe", Toast.LENGTH_SHORT).show();
                                    }


      }
    
answered by 04.04.2018 в 19:10