0% found this document useful (0 votes)
7 views12 pages

4.7 ListView

The document explains the concepts of Adapter and Adapter View in Android, highlighting their roles in efficiently displaying lists and grids of data. An Adapter acts as a bridge between a data source and the user interface, while Adapter Views like ListView and GridView facilitate the presentation of large datasets. It also details various attributes and examples for configuring ListView and GridView components in Android applications.

Uploaded by

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

4.7 ListView

The document explains the concepts of Adapter and Adapter View in Android, highlighting their roles in efficiently displaying lists and grids of data. An Adapter acts as a bridge between a data source and the user interface, while Adapter Views like ListView and GridView facilitate the presentation of large datasets. It also details various attributes and examples for configuring ListView and GridView components in Android applications.

Uploaded by

Rajwardhan Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Adapter and Adapter View

-Adapter and Adapter View are so popular, that every time you see any app with a List of items or Grid of items,
you can say for sure that it is using Adapter and Adapter View.
-Generally, when we create any List or Grid of data, we think we can use a loop to iterate over the data and then set
the data to create the list or grid. But consume a lot of time, making the app slow
• What is an Adapter?
-An adapter acts like a bridge between a data source and the user interface. It reads data from various data
sources, coverts it into View objects and provide it to the linked Adapter view to create UI components.
The data source or dataset can be an Array object, a List object etc.
• What is an Adapter View?
An Adapter View can be used to display large sets of data efficiently in form of List or Grid etc, provided to it by an
Adapter.

Suppose you have a dataset, like a String array with the following contents.

String days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

Now, what does an Adapter do is that it takes the data from this array and creates a View from this data and then, it gives this
View to an AdapterView. The AdapterView then displays the data in the way you want.
ListView
-List of scrollable items can be displayed in Android using ListView.
-It helps you to displaying the data in the form of a scrollable list.
Adapter: To fill the data in a ListView we 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.
Example-
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
Attributes of ListView:-
1. id: id is used to uniquely identify a ListView.

2. divider: This is a drawable or color to draw between different list items.


Example-
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="1dp"
/>
3. dividerHeight: This specify the height of the divider between list items. This could be in dp(density pixel),sp(scale
independent pixel) or px(pixel). In above example of divider we also set the divider height 1dp between the list items. The
height should be in dp,sp or px.
4. listSelector: listSelector property is used to set the selector of the listView. It is generally orange or Sky blue color mostly
but you can also define your custom color or an image as a list selector as per your design
Example-
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#f00"
android:dividerHeight="1dp"
android:listSelector="#0f0"/> <!--list selector in green color-->
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
}
}

1. parent -The AdapterView where the click happened.


2. view - The view within the AdapterView that was clicked (this will be a view provided by the adapter)
3. position - The position of the view in the adapter.
4. id - The row id of the item that was clicked.
Example-
GridView-
In android GridView is a view group that display items in two dimensional scrolling grid (rows and columns),
the grid items are not necessarily predetermined but they are automatically inserted to the layout using a ListAdapter.
Example-
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"/>
Attributes of GridView:
1.id: id is used to uniquely identify a GridView.
2.numColumns: numColumn define how many columns to show. It may be a integer value, such as “5” or auto_fit.
auto_fit is used to display as many columns as possible to fill the available space on the screen.
If we don’t specify numColumn property in GridView it behaves like a ListView with singleChoice.
Example-
<!-- numColumns example code -->
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="4"/> <!-- numColumns set to 4-->
3. horizontalSpacing: horizontalSpacing property is used to define the default horizontal spacing between columns. This could
be in pixel(px),density pixel(dp) or scale independent pixel(sp).
Example-
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="50dp"/><!--50dp horizontal space between grid items-->
4.verticalSpacing: verticalSpacing property used to define the default vertical spacing between rows. This should be in px, dp or
sp.
Example-
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:verticalSpacing="50dp"/><!--50dp vertical space set between grid items-->
5.columnWidth: columnWidth property specifies the fixed width of each column. This could be in px, dp or sp.
Example-
<!--columnWidth in Grid view code-->
<GridView
android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:columnWidth="80dp"
android:listSelector="#0f0"/><!--define green color for selected item-->
Example-

You might also like