0% found this document useful (0 votes)
20 views4 pages

Views and Adapters

The document discusses the use of ListView and ArrayAdapter in Android for displaying scrollable lists of data. It explains how Adapters serve as a bridge between data sources and views, detailing the types of Adapters available. Additionally, it provides a step-by-step guide on implementing a ListView using an ArrayAdapter with sample code snippets for clarity.

Uploaded by

amits.bhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

Views and Adapters

The document discusses the use of ListView and ArrayAdapter in Android for displaying scrollable lists of data. It explains how Adapters serve as a bridge between data sources and views, detailing the types of Adapters available. Additionally, it provides a step-by-step guide on implementing a ListView using an ArrayAdapter with sample code snippets for clarity.

Uploaded by

amits.bhel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

List View
Scrollview is ideal for screens where scrolling is required, but it is not efficient when
scroll view is used to render a larger data set. Instead you can use specialized
adapter views like ListView,GridView and Recycler View (Introduced in Android
Lollipop) for long lists.
1. ListView is an Android ViewGroup, used extensively to display the collection
of data in vertical scrollable rows.

2. The list items are automatically inserted to the list using an Adapter and
Adapter pulls data from data source source such as an array, cursor, etc.

2. Android Adapter
Adapter is acts as a bridge between data source and adapter views such as
ListView, GridView. Adapter iterates through the data set from beginning till the
end and generate Views for each item in the list.

Android SDK provides three different Adapter implementation, that


includes ArrayAdapter,CursorAdapter and SimpleAdapter. An ArrayAdapter
expects an Array or an List as input, while CursorAdapter accepts the instance of
Cursor and SimpleAdapter maps the static data defined in the resources. The type of
adapter that suits your app need is purely based on the input data type.
The BaseAdapter is the generic implementation for all of the three adapter types and
can be used for ListView, GridView or for Spinners.

You may directly use ArrayAdapter by passing array as input or create your own
customized class by extending BaseAdapter.
The image above, provides idea of customizable list views can be done using
adapters.

3. ListView Using ArrayAdapter


The simplest way for building list view is by using ArrayAdapter. Following are some
of the steps used to implement simple ListView using array adapter.

1. First step towards building list view is to identify the input data, which you
want to display in list. In this example, we will be using a static array of
strings.

2. Secondly, let us declare list view in activity layout. In our example, the
activity layout contains a list view inside linear layout.
Provide android:id="@+id/months_list" as ListView id.
3. Now finally, let us instantiate the the ArrayAdapter and set to ListView by
callingsetAdapter() method.
Following code snippet depicts the list view declaration inside activity layout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/months_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>

Now instantiate the ListView and Array adapter. Following code snippet depicts the
activity code.

ListActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListActivity extends Activity {


private String[] monthsArray = { "JAN", "FEB", "MAR",
"APR", "MAY", "JUNE", "JULY",
"AUG", "SEPT", "OCT", "NOV", "DEC" };

private ListView monthsListView;


private ArrayAdapter arrayAdapter;

/** Called when the activity is first created. */


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

monthsListView = (ListView)
findViewById(R.id.months_list);

// this-The current activity context.


// Second param is the resource Id for list layout
row item
// Third param is input array
arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, monthsArray);
monthsListView.setAdapter(arrayAdapter);
}
}

Output of the above code is as follows

You might also like