Adapter_Updated.pdf Android Adapter
Document Details
Uploaded by JubilantFrenchHorn287
Tags
Summary
This document provides a comprehensive overview of Android adapters. It explains how adapters act as bridges between data sources and user interfaces in Android applications, detailing different adapter types like BaseAdapter, ArrayAdapter, CustomArrayAdapter, SimpleAdapter, and CustomSimpleAdapter. The document also covers AdapterViews and how they efficiently display large datasets, utilizing strategies like rendering only visible items and reusing layouts for scrolling optimization. It also presents examples of using adapters with specific UI elements like Spinners and ListViews.
Full Transcript
Adapter What is an Adapter? An adapter acts like a bridge between a data source and the user interface. It reads data from various data sources, coverts it into View objects and provide it to the linked Adapter view to create UI components. The data source or dataset can be an Array object...
Adapter What is an Adapter? An adapter acts like a bridge between a data source and the user interface. It reads data from various data sources, coverts it into View objects and provide it to the linked Adapter view to create UI components. The data source or dataset can be an Array object, a List object etc. You can create your own Adapter class by extending the BaseAdapter class, which is the parent class for all other adapter class. Android SDK also provides some ready-to-use adapter classes, such as ArrayAdapter, SimpleAdapter etc. Adapters in Android 1. BaseAdapter – It is parent adapter for all other adapters 2. ArrayAdapter – It is used whenever we have a list of single items which is backed by an array 3. Custom ArrayAdapter – It is used whenever we need to display a custom list 4. SimpleAdapter – It is an easy adapter to map static data to views defined in your XML file 5. Custom SimpleAdapter – It is used whenever we need to display a customized list and needed to access the child items of the list or grid What is an Adapter View? An Adapter View can be used to display large sets of data efficiently in form of List or Grid etc, provided to it by an Adapter. An Adapter View is capable of displaying millions of items on the User Interface, while keeping the memory and CPU usage very low and without any noticeable lag. Different Adapters follow different strategies for this, but the default Adapter provided in Android SDK follow the following tricks: 1. It only renders those View objects which are currently on-screen or are about to some on-screen. Hence no matter how big your data set is, the Adapter View will always load only 5 or 6 or maybe 7 items at once, depending upon the display size. Hence saving memory. 2. It also reuses the already created layout to populate data items as the user scrolls, hence saving the CPU usage. Suppose you have a dataset, like a String array with the following contents. String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}; Now, what does an Adapter do is that it takes the data from this array and creates a View from this data and then, it gives this View to an AdapterView. The AdapterView then displays the data in the way you want. Therefore, you can take the data from a database or an ArrayList or any other data source and then, you can display that data in any arrangement. You can display it vertically (ListView), or in rows and columns (GridView), or in drop- down menu (Spinners), etc. Spinner with Array Adapter public class adapter_spinner extends AppCompatActivity implements AdapterView.OnItemSelectedListener { String[] country = { "India", "USA", "China", "Japan", "Other"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_adapter_spinner); Spinner spin = (Spinner) findViewById(R.id.spinner); spin.setOnItemSelectedListener(this); ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_spinner_item, country); // set simple layout resource file // for each item of spinner ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Set the ArrayAdapter (ad) data on the // Spinner which binds data to spinner spin.setAdapter(ad); } @Override public void onItemSelected(AdapterView adapterView, View view, int i, long l) { Toast.makeText(getApplicationContext(), country[i],Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView adapterView) { } } Same code with different approach public class MainActivity7 extends AppCompatActivity { String[] country = { "India", "USA", "China", "Japan", "Other"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main7); Spinner spin = (Spinner) findViewById(R.id.spinner); ArrayAdapter ad = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country); ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(ad); spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView adapterView, View view, int i, long l) { Toast.makeText(getApplicationContext(), country[i],Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView adapterView) { } }); } } Android ListView ListView is a ViewGroup which is used to display the list of scrollable of items in multiple rows and the list items are automatically inserted to the list using an adapter. Generally, the adapter pulls data from a sources such as an array or database and converts each item into a result view and that’s placed into the list. ListView is used when you have to show items in a vertically scrolling list. Best example of it is our device's Contact List. With ListView, user can easily browse the information, while scrolling up and down. You can set divider between every item and set its height and color as per your UI design. Inside a ListView, we can show list of Text items by using TextView, or pictures using ImageView, or any other view or a combination of views. As ListView is generally used to display a large set of data, hence it is not feasible to manually create list items for the complete data, hence Android provides us with special Adapter classes which can be used to supply data from datasets to ListView. We will define a ListView in the main layout XML file activity_main.xml. 1. We need a dataset and a View into which the dataset will be converted by the Adapter. 2. Here we have a simple Array with festivals names in it: String[] festivals = { "Diwali", "Holi", "Christmas", "Eid", "Baisakhi", "Halloween" }; As our data set has simple text values, so we can define a simple TextView to hold these values and populate the ListView. If our dataset would have had, an image and some text along with it, then we can also define a TextView along with an ImageView to display the data in the List. So now we will create a new XML file, with name list_item.xml in the layout folder, and add a TextView in it like this We have the MainActivity.java class, in which we have used an ArrayAdapter to create text views from the data in the array, and create a list by supplying those view objects to the ListView. public class MainActivity extends AppCompatActivity { ListView listView; MainActivity.java String[] festivals = { @Override "Diwali", public void onItemClick(AdapterView adapterView, View "Holi", view, int position, long l) { "Christmas", // TODO Auto-generated method stub "Eid", "Baisakhi", "Halloween" String value = "Happy " + adapter.getItem(position); }; @Override Toast.makeText(getApplicationContext(), value, protected void onCreate(Bundle savedInstanceState) { Toast.LENGTH_SHORT).show(); super.onCreate(savedInstanceState); } setContentView(R.layout.activity_main); }); } listView = (ListView)findViewById(R.id.listView); } final ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, festivals); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { Another Example MainActivity.java public class ListDisplay extends Activity { // Array of strings... String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry", "WebOS","Ubuntu","Windows7","Max OS X"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter adapter = new ArrayAdapter(this, R.layout.activity_listview, mobileArray); ListView listView = (ListView) findViewById(R.id.mobile_list); listView.setAdapter(adapter); } } Following will be the content of res/layout/activity_main.xml file Following will be the content of res/values/strings.xml to define two new constants ListDisplay Settings Following will be the content of res/layout/activity_listview.xml