Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Passing user-selected data to a detail activity

From: Android SDK: Local Data Storage

Video: Passing user-selected data to a detail activity

My sample application so far is using data that's imported from XML and then stored persistently in an SQLite database, and it's now capable of being displayed in either a rich display as we see here or in a pure text display. I'm going to add some new functionality now, and in fact, in the new version of the project--which is named DetailView-- the application now has a detail view. When the user touches or clicks on an item, that results in opening another activity, and right now that activity is populated with dummy data.

Passing user-selected data to a detail activity

My sample application so far is using data that's imported from XML and then stored persistently in an SQLite database, and it's now capable of being displayed in either a rich display as we see here or in a pure text display. I'm going to add some new functionality now, and in fact, in the new version of the project--which is named DetailView-- the application now has a detail view. When the user touches or clicks on an item, that results in opening another activity, and right now that activity is populated with dummy data.

I'll show you how I created that detail activity but then go on to the most important part, how to take a data object such as my tour object and prepare it so that it can be passed from one activity to another. First, let's take a look at the elements of this new functionality. In this version of the application, I've added a new layout file called tour_detail.xml. You can find it in the layout folder underneath the resources. This file uses nested LinearLayouts.

There's an ImageView control that's displaying the map and then a nested LinearLayout with an orientation of vertical that contains two text views for the title and the price. Then underneath all that, there's another text view nested within a ScrollView. This is for the description, a long bit of text on a smaller device, the ScrollView will allow the TextView to scroll up and down, so the user can see all of the text. To go along with that layout file, I have a new java class called TourDetailActivity.

This is where I've defined that dummy data. This class extends activity, and it's registered in the manifest file as a new activity. In the onCreate method, I'm populating the tour object with this dummy data and then calling the refreshDisplay method which contains all the code to display the data with the TextViews and the ImageView. This code is very similar to the code that I used to display data in the list items in the rich display. Finally, in the main activity class, I've added a new method called onListItemClick.

This method is called automatically from the list activity when the user selects an item. I'm creating a new intent object, populating it with the TourDetailActivity class and then starting the activity. So now my job is to take the selected data, the actual tour that the user selected, and pass it to the new detail activity. The first step is to get a reference to the selected tour. I can do that by using this position argument that's passed into onListItemClick.

That will tell me the position of the tour that was selected in the data collection. So above the Intent, I'll create a new tour object that I'll name tour, and I'll get its reference by calling tours.get and I'll pass in the position argument. So now I know what data I want to pass in. One way of passing the data would be to call the putExtra method of the Intent object a whole bunch of times. For example, after I create the Intent, I might call intent.putExtra, and then I would pass in a string as the key to the value, and then the actual value and I might call tour.getId, and I would call that method five times here, and then in the TourDetailActivity, I would call the getExtra method five times and put the tour object back together again.

That approach will definitely work, but a cleaner and more elegant approach is to do some work in the Tour class itself so that you can pass the entire object as a single object, and the right way to do this is to use an interface in Android called Parcelable. So let's go to the tour object that I've already defined. I'll shrink down my editor, and in my Package Explorer, I'll go to the model package and I'll open the class Tour.java.

Right now this class is following a classic data transfer object or JavaBean pattern. It has private properties and then getters and setters for each of those properties. And in addition, there's a two-string method that's being used in the pure text presentation to display the tour information. In order to make this tour object something that you can pass from one activity to the next, you need to implement the Parcelable interface. I'll go back up to the class declaration.

I'll add the implements keyword and then type par, press Ctrl+Spacebar, and select Parcelable. When I add that to the Tour declaration, I see an error on the right. I'll click on it and it tells me that I have to add unimplemented methods. I'll select that quick fix, then I'll scroll down to the bottom of the class and I see that two new methods have been created called describeContents and writeToParcel.

The describeContents method will be called by Android whenever it needs to find out what kind of special objects might be a part of this class. My class doesn't have any special objects. It's limited to primitive values and strings. I can return a value of zero and that basically says never mind. The writeToParcel method is a different story. Its job is to describe the different data elements and pass them to what's called the parcel destination and it will be called automatically whenever Android needs to get a flattened version of this object, something it can save while one activity is going away and another activity is coming to the screen.

We're getting to a point now, though, where there's going to be some significant typing. So I've added a typinghelp file to the project. I'll open that up and I'll expand it to Full Screen, and let's take a look at all of the code. First of all, there's a new default no arguments constructor method. The Tour class in its current state didn't have one of these and it's going to need it because there's going to be another version of the constructor method that receives a parcel argument.

This class is going to be called automatically whenever a tour object is being passed into an activity. The class will be instantiated and then values will be passed in, in something called the parcel. We then take those values and pass them to the private properties of the current instance of this class. Notice the order in which I'm calling these methods. That will become important in a moment. Here's that describeContents method again, and I'm only returning zero, which basically means I don't have anything other than simple values.

Here's a full implementation of the writeToParcel method. This method will be called when I'm passing data out of the activity. Remember I said to remember the order in which I was passing data in? You have to pass data in and data out in exactly the same order. So I have my data in the order of ID, title, description, price, and image here, and I'm using the same order in the writeToParcel method. That is absolutely critical. Finally, each class which implements the Parcelable interface must have a static field called CREATOR, the name of the field must be all uppercase, and it has to be of a datatype of Parcelable.Creator.

The data type of the generic declaration should match your data object. I'm using Tour in the generic declaration here and here and also when the methods create from parcel and newArray, both in the data that's being passed back and in the instantiation code within the methods. So all this code taken together will make my tour class something that can be passed from one activity to the next. As I mentioned, it's a lot of code, so I'm just going to copy it and paste it.

I'll select and copy. I'll come back to the Tour class. I'll remove the Override methods that were just created and instead paste in that code. When I paste in the code, I'll see I need to apply a few quick fixes to add import statements. So I'll move to each line that's displaying an error and press Command+1 on Mac or Ctrl+1 on Windows and select the Import quick fixes. I'll scan and make sure I don't have any other errors then I'll save that class.

Now I'll come back to the MainActivity class. I'll still use the putExtra method to pass the data into the Intent object, but now instead of an arbitrary key for the value, I must use the full package name and class name of the data object. Just as with any key value that you pass into putExtra, this will be a string. You can either pass in the entire package name or you can start with the sub-package underneath the default package of the project, and that's what I'll do.

I'll start with .model and then .tour and then I'll pass in the tour object that I just got a reference to up here. That's all I need to do. All of the difficult code is now in the Tour class, and all I have to do is get an object and pass it in and say what class it's an instance of. I'll save those changes, then I'll come to the TourDetailActivity class. I'm going to comment out the code that's creating the dummy tour object, and I'll replace it with the following code. First, I'll create a bundle object.

I'll press Ctrl+Spacebar just to make sure I've added the import for the class, and I'll name the object B and then I'll call getIntent().getExtras. Then to get the data from the bundle, I'll say tour = and then I'll call a method of the bundle object called getParcelable and I'll pass in the same string key, .model.tour, and that's it.

Once again, the code in the receiving activity is incredibly simple, all the hard stuff is in the Tour class and I'm ready to test. I'll run the application in the emulator again, and I'll click into an item and this time I see the actual data of the selected item. Remember, I mentioned that I'd be able to scroll up and down on a smaller device so I could see all of the text in the description. I'll go back a level to the My List Activity.

I'll scroll a bit and I'll select an item with a custom map, the Channel Islands Excursion, and I see that my Detail Activity correctly selects the right image as well. And again, I'll scroll up and down and show that it's all working correctly. So the most important new lesson in this exercise is how to take a class that's designed to contain data and turn it into something that's Parcelable. That is something that can be passed from one activity to the next to make it easy to move data around your Android application.

Show transcript

This video is part of

Image for Android SDK: Local Data Storage
Android SDK: Local Data Storage

29 video lessons · 5866 viewers

David Gassner
Author

 
Expand all | Collapse all
  1. 6m 34s
    1. Welcome
      50s
    2. What you should know before starting this course
      2m 11s
    3. Using the exercise files
      3m 33s
  2. 21m 6s
    1. Exploring local data storage options
      5m 58s
    2. Configuring Eclipse and the Android Developer Tools
      5m 29s
    3. Creating an Android virtual device
      3m 23s
    4. Creating a new Android project
      6m 16s
  3. 31m 30s
    1. Using preferences in Android apps
      5m 28s
    2. Defining preferences with Java
      9m 31s
    3. Defining shared preferences with an activity
      10m 50s
    4. Listening for changes to shared preferences
      5m 41s
  4. 51m 13s
    1. Creating and reading files in internal storage
      10m 18s
    2. Creating and reading JSON data files
      9m 48s
    3. Working with files in external storage
      11m 22s
    4. Parsing a read-only XML file with XmlPullParser
      11m 46s
    5. Parsing a read-only XML file with JDOM
      7m 59s
  5. 1h 8m
    1. Creating a new SQLite database
      2m 52s
    2. Defining a database with SQLiteOpenHelper
      12m 12s
    3. Managing the database with a DataSource class
      9m 38s
    4. Inserting data into a database table
      10m 29s
    5. Retrieving and displaying data
      11m 44s
    6. Importing data from XML to SQLite
      5m 15s
    7. Filtering and sorting data
      9m 27s
    8. Accessing a database from the command line
      6m 46s
  6. 42m 0s
    1. Improving the data display
      9m 29s
    2. Passing user-selected data to a detail activity
      11m 36s
    3. Working with multiple database tables
      10m 28s
    4. Deleting data from database tables
      10m 27s
  7. 49s
    1. Where to go from here
      49s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Android SDK: Local Data Storage.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.