I need to display all the identical records from Android Studio with SQLite

0

I have a database in an app in which I have registered the Names (VARCHAR) and an RFC (VARCHAR) of users. I have to do a search by entering a parameter in the two tables (Name and RFC), if there are repeated records, the list has to display all the records equal to the parameter that I insert. But my code only shows the first record that is equal to the parameter.

private void consultarSql() {
    SQLiteDatabase db=conn.getReadableDatabase();
    String[] parametros={campoId.getText().toString() , campoId.getText().toString() };
    Usuario usuario=null;

    try {
        Cursor cursor=db.rawQuery("SELECT "
                +Utilidades.CAMPO_ID+","
                +Utilidades.CAMPO_NOMBRE+","
                +Utilidades.CAMPO_TELEFONO+ ","
                +Utilidades.CAMPO_RFC+ ","
                +Utilidades.CAMPO_DIRECCION+","
                +Utilidades.CAMPO_PASSWORD+
                " FROM "+Utilidades.TABLA_USUARIO+" WHERE "+Utilidades.CAMPO_RFC+ " = ? OR " +Utilidades.CAMPO_NOMBRE+" = ? ",parametros);

        cursor.moveToFirst();
            usuario = new Usuario();
            usuario.setId(cursor.getInt(0));
            usuario.setNombre(cursor.getString(1));
            usuario.setTelefono(cursor.getString(2));
            usuario.setRFC(cursor.getString(3));
            usuario.setDireccion(cursor.getString(4));
            usuario.setPassword(cursor.getString(5));

            //System.out.println("Usuario encontrado con nombre: "+ usuario.getNombre());
            listaUsuario.add(usuario);



    }catch (Exception e){
        e.printStackTrace();
        Toast.makeText(getApplicationContext(),"No se encontro",Toast.LENGTH_LONG).show();
    }

}
    
asked by Axl HeadBanger 19.06.2018 в 06:30
source

1 answer

0

Regarding what you comment, I need to iterate the content of the cursor object, since with the query that you run the object you would already have all the data, it would look like this:

        if( cursor.moveToFirst() )
        {
            do{
                usuario = new Usuario();
                usuario.setId(cursor.getInt(0));
                usuario.setNombre(cursor.getString(1));
                usuario.setTelefono(cursor.getString(2));
                usuario.setRFC(cursor.getString(3));
                usuario.setDireccion(cursor.getString(4));
                usuario.setPassword(cursor.getString(5));

                listaUsuario.add(usuario);
            }while( cursor.moveToNext() );  
        }

Greetings!

    
answered by 19.06.2018 в 18:34