Register product for logged in user

Asked

Viewed 208 times

1

I’m getting the object through Activity's using putExtra and returning on the other screen with the getSerializableExtra.

I’ve already created the fk id_usuario in the product table for the field id_user of the user table.

My question now is:

How do I insert my product into the products table linked with the user id of the user table? How am I going to insert this product? Where do I pass the ID of the user I picked up in Activity to be inserted in it? Use in contentValues? In the 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;
}

1 answer

1

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

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.

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

Browser other questions tagged

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