Android_unit_4

4.1 Base Adapter

In Android, an adapter is a bridge between UI components and a data source. It helps fill data in the UI components, such as ListView, GridView, Spinner, etc. BaseAdapter is a common base class for a general implementation of an Adapter used in these views.


public class CustomAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return null;
    }

1. getCount()

The getCount() function returns the total number of items to be displayed in a list. It counts the value from an array list using the size() method or an array’s length.

2. getView(int i, View view, ViewGroup viewGroup)

This function is automatically called when the list item view is ready to be displayed or about to be displayed. In this function, we set the layout for list items using the LayoutInflater class and then add the data to the views like ImageView, TextView, etc.

3. getItem(int i):

This function is used to Get the data item associated with the specified position in the data set to obtain the corresponding data of the specific location in the collection of data items.

4. getItemId(int i):

As for the getItemId (int position), it returns the corresponding to the position item ID. The function returns a long value of item position to the adapter.

4.2 Array Adapter

ArrayAdapter is more simple and commonly used Adapter in android.

  • Whenever you have a list of single type of items which is backed by an array, you can use ArrayAdapter. For instance, list of phone contacts, countries or names.
  • By default, ArrayAdapter expects a Layout with a single TextView, If you want to use more complex views means more customization in grid items or list items, please avoid ArrayAdapter and use custom adapters.

4.3 ListView using Adapter

To fill the data in a ListViewwe simply use adapters. List items are automatically inserted to a list using an adapter that pulls the content from a source such as an arraylist, array or database.

4.4 GridView using Adapter

The GridView shows items in a two-dimensional scrolling grid. We can use the GridView together with an imageView to display series of images.

4.5 Photo Gallery using Adapter

In scenario we don’t want an image to appear suddenly when the user open the view, so we want to apply some animation to an image when transitions from one image to another .

4.6 Using Menu with Views

4.6.1 Option Menu

This MENU displays information related to the current activity. In android we activate the options menu by pressing the menu button.

4.6.2 Context Menu

This MENU displays information related to the particular view on an activity. In android we tap and hold a context menu to activate it.

4.6.3 Popup Menu

Android popup menu displays the menu below the anchor text if space is available otherwise above the anchor text. It disappears if you click outside the popup menu.

No comments:

Post a Comment