Register product for logged in user

1

I'm able to pass the object by Activity's using putExtra and returning on the other screen with getSerializableExtra .

I already created fk id_usuario in the products table for the id_user field of the users table.

My question now is:

How do I insert my product into the linked products table with the user id of the users table? How do I insert this product? Where do I pass the user ID I got in Activity to be inserted into it? Usage in contentValues ? In insert repository method? If you have an example it would help a lot!

public ContentValues contentValues(Reporte reporte) {
    ContentValues values = new ContentValues();
    values.put("id_user", reporte.getIdUsuario());
    values.put("tipo", reporte.getTipoReporte());
    values.put("descricao", reporte.getDescricaoReporte());
    values.put("status", reporte.getStatusReporte());
    values.put("data", reporte.getDataAbertura());
    values.put("hora", reporte.getHoraAbertura());
    values.put("latitude", reporte.getLatitude());
    values.put("longitude", reporte.getLongitude());
    values.put("endereco", reporte.getEndereco());
    return values;
}

public long insertReporte(Reporte novoReporte) {
    long id = 0;
    try {
        ContentValues values = contentValues(novoReporte);
        id = db.insert("reportes", null, values);
    } catch (Exception e) {
        Log.e("Erro: ", e.getMessage());
    }
    return id;
}
    
asked by anonymous 08.10.2015 / 15:02

1 answer

1

Good morning. In this case you inform the Product object what the user id is.

Example:

  public ContentValues contentValues(Produto produto) {
    ContentValues values = new ContentValues();
    values.put("id_produto", reporte.getIdProduto());
    values.put("nome", produto.getNome());
    values.put("descricao", produto.getDescricao());
    values.put("valor", produto.getValor());

    values.put("id_usuario", produto.getUsuario()); // Aki

    return values;
}

It would be interesting to present your Entities, to know how this relationship is.

Another tip is to use the framework ORMlite , very cool and helps a lot with that.

    
13.07.2016 / 14:09