Professional Documents
Culture Documents
T.Heba AL.Marwai
What is a list view?
• A ViewGroup that displays scrollable items.
3 Parts of a ListView
• The ListView itself
– A ViewGroup
• List items
– Each row/item in the list. Each item is a layout
consisting of a View or ViewGroup.
• Data for each item
ListItem
• Since the ListItem can be a ViewGroup, we
have the power to display very simple or
extremely complex layouts for each item.
– A TextView
– A TextView and ImageView
– A TextView and CheckBox
– A TextView and Button
– A TextView, ImageView, CheckBox, Button, and
RatingBar, etc
Where does the data come from?
• ListViews receive data via Adapters.
Honey I shrunk…
Pupper Master
ListView
Data source for Adapters
1. Arrays
2. Content Provider
– Used to get Calendar and Contact info from
phone.
3. Database Cursor
Types of Adapters
• ArrayAdapter
– Works with Arrays
– Can handle any Java Object as input
– Uses the .toString() method of the JavaObject to
obtain text for list item.
• SimpleCursorAdapter
– Works with a Content Provider and Database
Cursor
Hello World ListView Example
Layout Resource
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Code
package com.example.lecture2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_lecture);
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
Inflate the layout
public class ListLecture extends Activity {
for the Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_lecture);
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_lecture);
2
Creating Adapter: Parameter 3
// First paramenter - Context
// Second parameter - Layout for the list item or row
3
Creating Adapter: Parameter 4
// First paramenter - Context
// Second parameter - Layout for the list item or row
// Third parameter - ID of the TextView to which the data is
written
// Forth - the Array of data
4
Code
package com.example.lecture2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_lecture);
android:id="@android:id/text1"
Not so mysterious Layout and ID???
// First paramenter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is
written
// Forth - the Array of data
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
1. Context
Toast Example
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
2
1. Context
2. String message to print
Toast Example
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
3
1. Context
2. String message to print
3. Duration to display Toast
Toast Example
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
1. Context
2. String message to print
3. Duration to display Toast
4. Method to make the Toast show itself
Specify Toast Duration
• The Toast Object has two predefined
durations.
1. LENGTH_LONG ~3.5 seconds
2. LENGTH_SHORT ~2 seconds
Specify Location of Toasts
• A standard toast notification appears near the
bottom of the screen, centered horizontally.
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
Modifying Your List
• How do I add more items?
• How can I add a set of items?
• How do I clear all items?
ArrayAdapter Helper Methods
• add(T Object) – Adds the specified object to the end of the array.