Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Topics Limited Resources and why we need to consider them.

Similar presentations


Presentation on theme: "Android Topics Limited Resources and why we need to consider them."— Presentation transcript:

1 Android Topics Limited Resources and why we need to consider them.
The goal of ListView and ArrayAdapters The Basics of ListView and ArrayAdapters How ListView interacts with ArrayAdapter In-class App: Lab 5 Part 3

2 Why must we consider limited resources?
Mobile devices have limited resources. Limited memory is used to store music, text messages, photos and much more. A limited amount of memory must also be shared across the device’s operating system for running and stored apps. Result: There is a significant impact if one of these apps eats up a lot of memory. Apps running on the device slow down and sometimes crash.  

3 Limited memory – Consider the Friends Contact List
A user may have 1,000 names, along with a photo, an audio sound, and phone numbers assigned to each person. The device would expend a lot of memory creating, storing, and displaying this scrollable contact information. It is conceivable that a large part of the system's memory goes into creating and storing 1,000 contacts. Little memory is left and may cause other apps to stall.

4 Observation #1 Not all 1,000 contacts will be shown on the screen at the same time. In the image, only six contacts will fit on the screen at any given time.

5 Observation #2 The user may never scroll through the entire list of contacts. Creating all contact elements would take up memory resources. Anything above or below the visible contacts in the list do not need to exist.

6 Solution to Limited Memory
Reuse the small number of Views that need to appear on the screen at the same time. Use a limited number of pre-created Views and populate them with contact information. When a contact is no longer visible on the screen, the View can be used to display information for a different contact. This strategy is called View recycling. We can accomplish View recycling with a combination of a ListView and Adapter.

7 Why use a ListView and an Adapter?
The combination ListView and ArrayAdapter is one of the most efficient ways to handle large lists of data. ListViews and ArrayAdapters, are very common in Android development. Their purpose is to provide an efficient strategy for implementing the display of a long list of data. This combination allows developers to create a small number of views and recycle them. When we scroll upwards, we will be able to see the views below they will be generated quickly.

8 LinearLayout vs. ListView+Adapter
The LinearLayout needs to create a large number of list items. It can sometimes be expensive to inflate from XML. The ListView and Adapter recycle a small number of inflated list items.

9 ListView ListView is a smart View that has been coded by Android.
It is smart because it knows when Views at the bottom or top of the screen have been scrolled off the screen.

10 Basics of ListView +Adapter
The strength of ListView and ArrayAdapters is their ability to recycle views quickly. When a view is no longer visible on screen, it is reused by switching out the data. It recycling tends to be fast because time is not wasted on creating the entire view from scratch.

11 Scrap Views and ListView +Adapter
Extra views are placed in a scrap pile that can be recycled and reused, these are referred as Scrap Views. Scrap views are views that are not currently being shown on the screen, they are available for reuse later.

12 How the ListView interacts with the ArrayAdapter
A ListView is populated by the ArrayAdapter. Without the Adapter the ListView is just an empty container. The ArrayAdapter holds onto the set of data that will be displayed. The ArrayAdapter knows how to translate this set of data, or adapt it, into a ListView item. The ArrayAdapter knows how many items that the ListView expects to display.

13 How a ListView item is created:
The ListView calls the getView() method on the ArrayAdapter. At the start, the current position in the list is 0 – the user starts with the first item. The ArrayAdapter checks the internal source of data and creates a list item View to return to the ListView.

14 What happens when ListView items are no longer visible?
Items that have been scrolled off the screen will end up in the scrap pile. Scrap pile items are limited and will eventually be reused when a request for a new list item is made.

15 How the the ListView and ArrayAdapter communicate
TheListView will request the list item view for a certain position in the list. The ListView will identify a reusable View from the Scrap Pile. The ArrayAdapter uses the recycled view from the Scrap Pile by placing data inside it. The previous data is overwritten. The ArrayAdapter can then easily insert the view in the correct spot in the ListView as the user scrolls up or down.

16 Example 1: Bobo and Tilly, the first two list items, are no longer visible. Bobo and Tilly will be added to a Scrap Pile.

17 Example 2:

18 Simple Practice Problem
Numbers 1-30 in a Scrolling list using ListView + ArrayAdapter

19 Simple Practice Problem
Part 1: Create the main_layout.xml

20 Part 1 Solution : Create the main_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

21 Part 2: Code MainActivity.java
TASK 1: Create an data structure and populate it with a collection of Strings. HINT: Use a for loop to fill an ArrayList with a series of numbers formatted as Strings. TASK 2: Create an ArrayAdapter. Research the instantiation requirements for a basic usage of an ArrayAdapter populated with simple data. HINT: Explore android.R.layout.simple_list_item_1 TASK 3: Create a reference to the ListView component in the Activity layout. TASK 4: Link the ListView component with the ArrayAdapter.


Download ppt "Android Topics Limited Resources and why we need to consider them."

Similar presentations


Ads by Google