Adapter and adapter views that are used in android
JinalBhagat2
36 views
10 slides
Jul 15, 2024
Slide 1 of 10
1
2
3
4
5
6
7
8
9
10
About This Presentation
Adapter and adapter views that are used in android
Size: 59.87 KB
Language: en
Added: Jul 15, 2024
Slides: 10 pages
Slide Content
Understanding Adapters and Adapter Views Prof. Jinal Bhagat
An adapter view, as its name suggests, is a View object. This means, you can add it to your activities the same way you add any other user interface widget. However, it is incapable of displaying any data on its own. Its contents are always determined by another object, an adapter. In this tutorial, I show you how to create adapters and use them to feed different types of adapter views such as ListView and GridView .
What Is an Adapter ? An adapter is an object of a class that implements the Adapter interface. It acts as a link between a data set and an adapter view, an object of a class that extends the abstract AdapterView class. The data set can be anything that presents data in a structured manner. Arrays, List objects, and Cursor objects are commonly used data sets. An adapter is responsible for retrieving data from the data set and for generating View objects based on that data. The generated View objects are then used to populate any adapter view that is bound to the adapter. You can create your own adapter classes from scratch, but most developers choose to use or extend adapter classes provided by the Android SDK, such as ArrayAdapter and SimpleCursorAdapter . In this tutorial, we focus on the ArrayAdapter class.
How Do Adapter Views Work ? Adapter views can display large data sets very efficiently. For instance, the ListView and GridView widgets can display millions of items without any noticeable lag while keeping memory and CPU usage very low. How do they do that? Different adapter views follow different strategies. However, here's what most of them usually do. They render only those View objects that are either already on-screen or that are about to move on-screen. This way, the memory consumed by an adapter view can be constant and independent of the size of the data set. They also allow developers to minimize expensive layout inflate operations and recycle existing View objects that have move off-screen. This keeps CPU consumption low.
Creating an ArrayAdapter To create an adapter, you need the following: a data set a resource file containing the layout of the generated View objects Additionally, because the ArrayAdapter class can only work with strings, you need to make sure the layout of the generated View objects contains at least one TextView widget.
Step 1: Create the Data Set The ArrayAdapter class can use both arrays and List objects as data sets. For now, let's use an array as the data set. 1 String [] cheeses = { 2 "Parmesan" , 3 "Ricotta" , 4 "Fontina" , 5 "Mozzarella" , 6 "Cheddar" 7 };
Step 2: Create the Resource File Create a new layout XML file whose root element is a LinearLayout and name it item.xml. Drag and drop a Large text widget in it and set the value of its id attribute to cheese_name . The layout XML file should look like this: 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android= "https://schemas.android.com/apk/res/android" 3 android:orientation= "vertical" android:layout_width= "match_parent" 4 android:layout_height= "match_parent" 5 android:padding= "@dimen/activity_horizontal_margin" > 6 < TextView 7 android:layout_width = " wrap_content " 8 android:layout_height = " wrap_content " 9 android:textAppearance = "? android:attr / textAppearanceLarge " 10 android:text = "Large Text" 11 android:id = "@+id/ cheese_name " /> 12 </ LinearLayout >
Step 3: Create the Adapter In your activity, create a new instance of the ArrayAdapter class using its constructor. As its arguments, pass the name of the resource file, the identifier of the TextView , and a reference to the array. The adapter is now ready. 1 ArrayAdapter < String > cheeseAdapter = 2 new ArrayAdapter < String >( this , 3 R . layout . item , 4 R . id . cheese_name , 5 cheeses 6 );
Step 4 - Creating a List To display a vertically scrollable list of items, you can use the ListView widget. To add the widget to your activity, you can either drag and drop it inside the activity's layout XML file or create it using its constructor in your Java code. For now, let's do the latter. ListView cheeseList = new ListView ( this ); Usually, no other user interface widgets are placed inside a layout that contains a ListView . Therefore, pass the ListView to the setContentView () method of your activity so that it takes up the entire screen. setContentView ( cheeseList );