List View

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

Lecture 10

LIST VIEW

UI Overview
A View is an object that draws something on the screen that the user can interact with. A View Group is an object that holds other View (and ViewGroup) objects in order to define the layout of the interface. LinearLayout and RelativeLayout are typical examples

Layout with Adapters

Data Adapters
Android framework for providing linking mechanism between raw data and view objects

AdapterView Class
A subclass of ViewGroup whose child Views are determined by an Adapter Gallery, ListView, and Spinner are examples of AdapterView

AdapterView objects have two main responsibilities:


Filling the layout with data & Handling user selections

Adapters and Data Sources


Adapters: Array Adapter Common methods: add() , clear(), addAll() Cursor Adapter SimpleArrayAdapter Custom Adapter Data sources could be String Array written in Java String Array Resource Database or content provider ..

ListActivity
ListActivity class can be used if your activity only holds ListView. It does not require assignment to a layout via the setContentView() method

In this case you can directly call the methods such as


setListAdapter(adapter);

Responding to Item Selection


lv.setOnItemClickListener( new AdapterView.OnItemClickListener() {

public void onItemClick(


AdapterView<?> arg0, View arg1, int position,long id) { Toast.makeText(getApplicationContext(), "Item " + position +" Selected", Toast.LENGTH_SHORT ) .show(); } });

ListView Demo

Creating Custom List Views


Implement your own adapter Override getView method

Adding Items to the list dynamically


items.add(newItem); adapter.notifyDataSetChanged();

For More About Adapters


http://www.vogella.com/articles/AndroidListView/article.html http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

You might also like