Wednesday, 16 November 2011

Android Adapters


        
                     

An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

An AdapterView is a view whose children are determined by an Adapter.

Some examples of AdapterViews are ListView, GridView, Spinner and Gallery.

There are several types or sub-classes of Adapter:

ListAdapter: Extended Adapter that is the bridge between a ListView and the data that backs the list. Frequently that data comes from a Cursor, but that is not required. The ListView can display any data provided that it is wrapped in a ListAdapter.


ArrayAdapter: A ListAdapter that manages a ListView backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.


Example:



package com.project.ListView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewActivity extends Activity implements OnItemClickListener {
      ListView listview;
      ArrayAdapter<?> aa;
      String[] list2 = { "murali", "Android", "technology", "java" };

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            listview = (ListView) findViewById(R.id.listView1);

            aa = new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_1, list2);

            listview.setAdapter(aa);
            listview.setOnItemClickListener(new OnItemClickListener() {

                  @Override
                  public void onItemClick(AdapterView<?> arg0, View arg1,
                              int position, long arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(),
                                    "Clicked At " + position, 1).show();
                  }
            });
      }

      @Override
      public void onItemClick(AdapterView<?> AA, View list, int position,
                  long arg3) {
            // TODO Auto-generated method stub

      }

}


CursorAdapter: Adapter that exposes data from a Cursor to a ListView widget. The Cursor must include a column named "_id" or this class will not work.


HeaderViewListAdapter: ListAdapter used when a ListView has header views. This ListAdapter wraps another one and also keeps track of the header views and their associated data objects.

This is intended as a base class; you will probably not need to use this class directly in your own code.


ResourceCursorAdapter: An easy adapter that creates views defined in an XML file. You can specify the XML file that defines the appearance of the views.


SimpleAdapter: An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list.



// get messages from service
      ArrayList smsMessages = service.getSmsCalls();
       
      // initialize the List of Maps
      List<map>&gt; list = new ArrayList<map>&gt;();
       
      // iterate over all messages
      // create a map for each message
      // fill the map with data
      for (Call c: smsMessages) {
            Map map = new HashMap();
            map.put("number", c.getTo());
            map.put("date", DateParser.getTimeString(c.getBegin()));
            map.put("price", "€ " + c.getPrice());
            list.add(map);
      }
       
      // the from array specifies which keys from the map
      // we want to view in our ListView
      String[] from = {"number", "date", "price"};
       
      // the to array specifies the TextViews from the xml layout
      // on which we want to display the values defined in the from array
      int[] to = {R.id.callog_detail_sms_number, R.id.callog_detail_sms_date, R.id.callog_detail_sms_price};
       
      // get a reference to the ListView
      ListView lv = (ListView)findViewById(R.id.callog_detail_listview);
       
      // create the adapter and assign it to the listview
   SimpleAdapter adapter = new SimpleAdapter(this.getApplicationContext(), list, R.layout.callog_detail_sms, from, to);

      lv.setAdapter(adapter);
      </map></map>
}


SimpleCursorAdapter: An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views.


SpinnerAdapter: Extended Adapter that is the bridge between a Spinner and its data. A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.


WrapperListAdapter: List adapter that wraps another list adapter. The wrapped adapter can be retrieved by calling getWrappedAdapter().

Itroduction To Android


What is android ?
                Android is a software stack for mobile devices that includes an operating system, middleware and key applications.


        One of the most widely used mobile OS these days is ANDROID. Android is a software bunch comprising not only operating system but also middleware and key applications. Android Inc was founded in Palo Alto of California, U.S. by Andy Rubin, Rich miner, Nick sears and Chris White in 2003. which is later on purchased by Google in 2005. Presently, Google and Open handset alliance are owners of Android operating system.  After original release there have been number of updates in the original version of Android.

               Android is a powerful Operating System supporting a large number of applications in Smart Phones. These applications make life more comfortable and advanced for the users. Hardwares that support Android are mainly based on ARM architecture platform. Some of the current features and specifications of android are:


Features in Android:




Technical overview to Android

Android is feature rich operating system for the mobile devices, which is developed by utilizing various programming languages,databases, frameworks, Libraries. This multipurpose operating system can be used in various mobile devices like netbooks, tablet computers and obviously in cellular phones. Giving a technical view that reveals an amazing fact of 12 million lines of code such as the programming languages in its coding are 2.8 million lines of C, Java holds 2.1 million lines and C++ with 1.75 million lines. The relational database management system SQLite is used for data storing. Android operating system software stack is composed of Java libraries, C libraries, SGL graphic engine, Webkit layout engine, Bionic libc, OpenGL ES 2.0 3D graphics API and SSL. There are various features for the Android handset user as well as Android developer / programmer. Few very common features are:
    • Application framework enabling reuse and replacement of components
    • Dalvik virtual machine optimized for mobile devices
    • Integrated browser based on the open source WebKit engine
    • Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)
    • SQLite for structured data storage
    • Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)
    • GSM Telephony (hardware dependent)
    • Bluetooth, EDGE, 3G, and WiFi (hardware dependent)
    • Camera, GPS, compass, and accelerometer (hardware dependent)
    • Rich development environment including a device emulator, tools for debugging, memory and performance profiling, and a plugin for the Eclipse IDE

Android comes with an Android market which is an online software store. It was developed by Google. It allows Android users to select, and download applications developed by third party developers and use them. There are around 2.0 lack+ games, application and widgets available on the market for users.

Android applications are written in java programming language. Android is available as open source for developers to develop applications which can be further used for selling in android market. There are around 200000 applications developed for android with over 3 billion+ downloads. Android relies on Linux version 2.6 for core system services such as security, memory management, process management, network stack, and driver model. For software development, Android provides Android SDK (Software development kit).


Applications:

These are the basics of Android applications:
•      Android applications are composed of one or more application components (activities, services, content providers, and broadcast receivers)
•      Each component performs a different role in the overall application behavior, and each one can be activated individually (even by other applications)
•      The manifest file must declare all components in the application and should also declare all application requirements, such as the minimum version of Android required and any hardware configurations required
•      Non-code application resources (images, strings, layout files, etc.) should include alternatives for different device configurations (such as different strings for different languages)


Google, for software development and application development, had launched two competitions ADC1 and ADC2 for the most innovative applications for Android. It offered prizes of USD 10 million combined in ADC1 and 2. ADC1 was launched in January 2008 and ADC 2 was launched in May 2009. These competitions helped Google a lot in making Android better, more user friendly, advanced and interactive.


Other Mobile Operating Systems:

There are different other Mobile operating systems also present in market in competition with Android. Apple's iOS and Windows Phone give strong competition to Android. A simple comparison between features and specifications of latest version of Android and other Operating Systems can be seen in the table given below.



According to Canalys, In Q2 2009 Android had 2.8% market share which had grown to 33% market share by Q4 2010 which made Android leader of smart phone OSs worldwide. The market share for commonly used mobile OSs is shown in the following pie chart.



     A large no of mobile companies are using Android. A list of companies supporting Android in their hardware is:
·         Acer Inc
·         ALCATEL ( TCL corporation )
·         Bluelans Communications
·         NCE casio Mobile Communications
·         Cherry Mobile
·         CSL
·         Dell
·         Garmin
·         Geeks Phone
·         General Mobile
·         High screen
·         HKC
·         HTC coroporation
·         Huawei
·         I-mobile
·         Lenovo
·         LG
·         Motorola
·         Samsung
·         Sony Ericsson
·         Videocon
·         ZTE



J2ME
Android
Platform like J2SE to development J2ME apps
Operating System with customized Java API to develop mobile apps
Kilobyte Virtual Machine (KVM)
Dalvik Virtual Machine (DVM)
Limited access to the native OS
Full access to the native OS
Limited GUI components
Many more and complex GUI components
Not easy to integrate with 3rd party applications
Has easy integration to Google's cloud services (GTalk, Maps, Gmail…)

                     


Components of Android are:
         1. Activities
         2. Services
         3. Broadcast Receivers
         4. Content Providers.

          Activity is the basic building block of every visible android application. It provides the means to render a UI. Every screen in an application is an activity by itself. Though they work together to present an application sequence, each activity is an independent entity.

        Service is another building block of android applications which does not provide a UI. It is a program that can run in the background for an indefinite period.

          Broadcast Receiver is yet another type of component that can receive and respond to any broadcast announcements. 

          Content Providers are a separate league of components that expose a specific set of data to applications.

          Intents 
are messages that are passed between components. Intent is a bundle of information, a passive data structure that holds an abstract description of the operation to be performed.
    • Intent is a bundle of information, a passive data structure that holds an abstract description of the operation to be performed. (or in the case of broadcasts, a description of an event that has happened and is being announced).
NOTE: Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.