1

I am making a recipe app. So my listview contains all the dishes. When the user clicks on, say "burger", I want to open the recipe for burger. However, should I create a new activity for every dish? Is it possible to create a template activity and store all the recipes in a database, (including images)?

Thanks!

user2153984
  • 183
  • 1
  • 3
  • 10

4 Answers4

0

I'm not providing code because there are a few different things that you need to learn in your own time.

First of all, don't make a new activity class for each recipe. Have one RecipeActivity which takes an ID. To pass the ID to the activity, use intent extras. Example here: Simple example for Intent and Bundle

As for the database, use SQLite. Example here: http://www.codeofaninja.com/2013/02/android-sqlite-tutorial.html

Community
  • 1
  • 1
OrhanC1
  • 1,415
  • 2
  • 14
  • 28
  • Thanks! However, for each dish, I will have a bunch of images besides texts. How do you store those images in the database? – user2153984 Jul 20 '14 at 13:30
  • Top answer explains it well: http://stackoverflow.com/questions/15935460/sqlite-and-storing-images – OrhanC1 Jul 20 '14 at 13:42
  • How can I store the information in the database before publishing it, so that the data is packed in the published app? Thanks! – user2153984 Jul 20 '14 at 14:12
0

I suggest you to pass the ID of the clicked item to a controller, this takes the ID and searches all the information requiered for the receipe with that ID in a database (you should use ormlite framework which is really simple) and then start an activity that displays the receipe which has nothing else to do than display the data it receives it.

This way, your portability increases, your DisplayReceipeActivity (if you may) is clean and only does what it is supposed to do: display data, and your controller does all the work.

Also, I would create a model class called Receipe which can hold all the attributes of a receipe, and can also provide public methods such as createReceipeFromDatabase(ID), etc.

Also, you should read something about design patterns and OOP concepts before you dive into writing complex applications.

I hope it helped, good luck!

Community
  • 1
  • 1
DDsix
  • 1,966
  • 1
  • 18
  • 23
0

In general it depends on your app and you should weigh the pros and cons of using fragment and activity for exactly your app.

For instance if activity which contains your list view with dishes has Navigation drawer and this drawer should be accessible when user see a recipe then it's obvious to show recipe in a fragment rather than in new activity where navigation drawer will not be visible.

Other important thing is the lifecycle of fragment is trickier than lifecycle of activity. (check it our here: https://github.com/xxv/android-lifecycle)

And the main difference between activity and fragment that fragment could be used inside activity but not vice versa. So if in the future you need to put recipe in some other part of your app it will be easier to do with fragment.

eleven
  • 6,779
  • 2
  • 32
  • 52
0

Create a TemplateActivity and when you click on the list item pass the row id to the TemplateActivity as follow

Intent i = new Intent(context,TemplateActivity.class);
i.putExtra("rowid", rowID);
context.startActivity(i);

And in the TemplateActivity get the row id as follow

Intent i = getIntent();
int rowID = i.getIntExtra("rowid", 0);

Then you can query the database using the row id to get the data

Omar Albelbaisy
  • 827
  • 8
  • 15
  • Thanks! However, for each dish, I will have a bunch of images besides texts. How do you store those images in the database? – user2153984 Jul 20 '14 at 13:30
  • You can store images in SQLite database as BLOB, check this http://stackoverflow.com/questions/7512019/how-to-save-images-into-database – Omar Albelbaisy Jul 20 '14 at 13:54