Jan 16, 2011

ListActivity and onListItemClick

ListActivity: An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

protected void onListItemClick (ListView l, View v, int position, long id)
Since: API Level 1

The method onListItemClick() will be called when an item in the list is selected. Subclasses should override. Subclasses can call getListView().getItemAtPosition(position) if they need to access the data associated with the selected item.
l: The ListView where the click happened
v: The view that was clicked within the ListView
position: The position of the view in the list
id: The row id of the item that was clicked

ListActivity and onListItemClick

package com.AndroidListActivity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListActivity extends ListActivity {

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

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DayOfWeek));

protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);

Toast.makeText(getBaseContext(), (CharSequence) getListView().getItemAtPosition(position), Toast.LENGTH_LONG).show();


No comments:

Post a Comment

Infolinks In Text Ads