How can I insert a drawable element (in this case an image) in a java object?
In this case here:
public Receta(String nombre, String elaboracion, String dificultad, String ingredientes, Drawable imagen) {
this.nombre = nombre;
this.elaboracion = elaboracion;
this.dificultad = dificultad;
this.ingredientes = ingredientes;
}
If the recipes are fixed, that is, the images are packaged in the application (apk) in the folder
res/drawable
, then you can save the reference to the image with itsR.drawable.<nombre>
, which is an int field.Then if, for example, you have the image
R.drawable.fideos_con_tuco
inres/drawable
:When you want to use the image, for example in an ImageView:
imageView.setImageResource(receta.getImagen()); //supongo que tendrás un getImagen() en Receta.
On the other hand, if it is the case that they are dynamic recipes, that is to say that it is the user who creates them, then the images will not be in
res/drawable
and the way to resolve the issue is totally different, since you would have to save a reference to where the app saves the image, presumably downloaded from a server.From your question it also seems to me that your case is the first.