0

Despite checking many questions in stackoverflow and google, i could not find the solution to this problem.

Can someone post and explain a sample code how to use parcelable in Android with multiple data and how it will be passed and displayed in an xml layout with loop.

E.g. for Restaurant Activity, it will pass and display the below items to Restaurant Menu Activity.

  1. Restaurant Name
  2. Restaurant Address
  3. Restaurant Menu Category ("American Breakfast")
    • American Breakfast 1, American Breakfast 2, American Breakfast 3...
  4. Restaurant Menu Category ("Waffles")
    • Waffles 1, Waffles 2, Waffles 3 etc...

and so the category and the food items continues...

Thanks!

2 Answers2

0

Please check the link When and how to implement Parcelable Vs. Serializable?

It shows how to generate a parcelable POJO automatically.

To communicate data to Restaurant Menu Activity do the following

Intent i = new Intent(fromActivity,toActivity);
i.putExtra("name_of_extra", (Parcelable) myParcelableObject);
startActivity(i);

And in your Restaurant Menu Activity

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");

Hope this helps.

Community
  • 1
  • 1
Krunal
  • 103
  • 4
0

Using Parcelable doesn't seem to be a right approach for your case, from architecture point of view. Most likely such restaurant data should be stored in SQLite DB, in tables. Data structures (data model) is a core for such apps, you probably need to design DB scheme first of all, then you'll be using Activities/Views to display that data, enter new data, and so on.

DB will keep entities in tables (data model), and when you need to operate with an entity (ie a restaurant menu) you just send id of that entity, in this particular case via Intent, most likely it will be a single integer, no need to parcel every attribute of the entity - in the Activity being launched you get the id from Intent's extra, and using that id the activity retrieves the entity's attributes from SQLite DB and puts values to appropriate views, this is an ordinary routine.

When you need to show many records, you'll probably use an Adapter, its purpose is to map data from source table into views, particularly how to render a record from DB into a view (probably with subviews) which will represent that record. Google for tutorials about adapters, they are used almost always when there are tables on screen.

EDITED - you need to look at Android's ListView control, read guide here: http://www.vogella.com/tutorials/AndroidListView/article.html

Tables are used in iOS, my mistake.

Mixaz
  • 4,068
  • 1
  • 29
  • 55