• Latest Code...

    Filling an adapter view with data in android

    Filling an adapter view with data in android have two methods.

    You can populate an AdapterView such as ListView or GridView by binding the AdapterView instance to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.

    Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView. T

    1. ArrayAdapter
    2. SimpleCursorAdapter

    1. ArrayAdapter

    Use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView.
    For example, if you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array:
    ArrayAdapter adapter = new ArrayAdapter<String>(this, 
    .R.layout.simple_list_item_1, myStringArray);
    The arguments for this constructor are:
    • Your app Context
    • The layout that contains a TextView for each string in the array
    • The string array
    Then simply call setAdapter() on your ListView:
    ListView listView = (ListView) findViewById(R.id.listview);

    To customize the appearance of each item you can override the toString() method for the objects in your array. Or, to create a view for each item that's something other than a TextView (for example, if you want an ImageView for each array item), extend the ArrayAdapter class and override getView() to return the type of view you want for each item.

    Example 1  Example 2  Example 3

    2. SimpleCursorAdapter
    Use this adapter when your data comes from a Cursor. When using SimpleCursorAdapter, you must specify a layout to use for each row in the Cursor and which columns in the Cursorshould be inserted into which views of the layout. For example, if you want to create a list of people's names and phone numbers, you can perform a query that returns a Cursor containing a row for each person and columns for the names and numbers. You then create a string array specifying which columns from the Cursor you want in the layout for each result and an integer array specifying the corresponding views that each column should be placed:
    String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME, 
    int[] toViews = {R.id.display_name, R.id.phone_number};
    When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the Cursor containing the results, and these two arrays:
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
    .layout.person_name_and_number, cursor, fromColumns, toViews, 0);
    ListView listView = getListView();
    The SimpleCursorAdapter then creates a view for each row in the Cursor using the provided layout by inserting each fromColumns item into the corresponding toViews view.
    If, during the course of your application's life, you change the underlying data that is read by your adapter, you should call notifyDataSetChanged(). This will notify the attached view that the data has been changed and it should refresh itself.

    Example 1  Example 2  Example 3

    Handling click events

    You can respond to click events on each item in an AdapterView by implementing the AdapterView.OnItemClickListener interface. For example:

    // Create a message handling object as an anonymous class.
    private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
    // Do something in response to the click


    Sources: http://developer.android.com/guide/topics/ui/declaring-layout.html

    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace