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().

Comments

Popular posts from this blog

How To Run Compatibility Test Suite (CTS) ?

NFC : Reading a MiFare Classic 1K from Android Devices

What is the directory structure of android AOSP root tree?