public abstract class WloBaseListActivity<M extends BaseModel> extends WloBaseActivity
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.
Screen Layout
ListActivity has a default layout that consists of a single, full-screen list
in the center of the screen. However, if you desire, you can customize the
screen layout by setting your own view layout with setContentView() in
onCreate(). To do this, your own view MUST contain a ListView object with the
id "@android:id/list" (or R.id.list if it's in code)
Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.
The following code demonstrates an (ugly) custom screen layout. It has a list with a green background, and an alternate red "no data" message.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
Row Layout
You can specify the layout of individual rows in the list. You do this by specifying a layout resource in the ListAdapter object hosted by the activity (the ListAdapter binds the ListView to the data; more on this later).
A ListAdapter constructor takes a parameter that specifies a layout resource for each row. It also has two additional parameters that let you specify which data field to associate with which object in the row layout resource. These two parameters are typically parallel arrays.
Android provides some standard row layout resources. These are in the
R.layout class, and have names such as simple_list_item_1,
simple_list_item_2, and two_line_list_item. The following layout XML is the
source for the resource two_line_list_item, which displays two data
fields,one above the other, for each list row.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
You must identify the data bound to each TextView object in this layout. The syntax for this is discussed in the next section.
Binding to Data
You bind the ListActivity's ListView object to data using a class that
implements the ListAdapter interface.
Android provides two standard list adapters:
SimpleAdapter for static data (Maps),
and SimpleCursorAdapter for Cursor
query results.
The following code from a custom ListActivity demonstrates querying the Contacts provider for all contacts, then binding the Name and Company fields to a two line row layout in the activity's ListView.
public class MyListAdapter extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
// We'll define a custom screen layout here (the one shown above), but
// typically, you could just use the standard ListActivity layout.
setContentView(R.layout.custom_list_activity_view);
// Query for all people contacts using the Contacts.People convenience class.
// Put a managed wrapper around the retrieved cursor so we don't have to worry about
// requerying or closing it as the activity changes state.
mCursor = People.query(this.getContentResolver(), null);
startManagingCursor(mCursor);
// Now create a new list adapter bound to the cursor.
// SimpleListAdapter is designed for binding to a Cursor.
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
android.R.layout.two_line_list_item, // Specify the row template to use (here, two columns bound to the two retrieved cursor
rows).
mCursor, // Pass in the cursor to bind to.
new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
new int[]); // Parallel array of which template objects to bind to those columns.
// Bind to our new adapter.
setListAdapter(adapter);
}
}
setListAdapter(android.support.v4.widget.SimpleCursorAdapter),
ListView| Modifier and Type | Field and Description |
|---|---|
protected android.support.v4.widget.SimpleCursorAdapter |
adapter |
protected static Map<Class,BaseModel> |
drawerItems |
static String |
INTENT_EXTRA_PARENT_MODEL |
protected android.widget.ListView |
mDrawerList |
protected android.support.v4.app.ActionBarDrawerToggle |
mDrawerToggle |
protected android.widget.ListView |
mList
This field should be made private, so it is hidden from the SDK.
|
protected BaseModel |
parentModel |
static int |
REQUEST_EDIT_MODEL |
protected WloSqlOpenHelper |
woh |
downloadingApkId, downloadManager, downloadReceiver, mMessenger, mServiceMessengerDEFAULT_KEYS_DIALER, DEFAULT_KEYS_DISABLE, DEFAULT_KEYS_SEARCH_GLOBAL, DEFAULT_KEYS_SEARCH_LOCAL, DEFAULT_KEYS_SHORTCUT, FOCUSED_STATE_SET, RESULT_CANCELED, RESULT_FIRST_USER, RESULT_OKACCESSIBILITY_SERVICE, ACCOUNT_SERVICE, ACTIVITY_SERVICE, ALARM_SERVICE, AUDIO_SERVICE, BIND_ABOVE_CLIENT, BIND_ADJUST_WITH_ACTIVITY, BIND_ALLOW_OOM_MANAGEMENT, BIND_AUTO_CREATE, BIND_DEBUG_UNBIND, BIND_IMPORTANT, BIND_NOT_FOREGROUND, BIND_WAIVE_PRIORITY, CLIPBOARD_SERVICE, CONNECTIVITY_SERVICE, CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESTRICTED, DEVICE_POLICY_SERVICE, DOWNLOAD_SERVICE, DROPBOX_SERVICE, INPUT_METHOD_SERVICE, INPUT_SERVICE, KEYGUARD_SERVICE, LAYOUT_INFLATER_SERVICE, LOCATION_SERVICE, MEDIA_ROUTER_SERVICE, MODE_APPEND, MODE_ENABLE_WRITE_AHEAD_LOGGING, MODE_MULTI_PROCESS, MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITEABLE, NFC_SERVICE, NOTIFICATION_SERVICE, NSD_SERVICE, POWER_SERVICE, SEARCH_SERVICE, SENSOR_SERVICE, STORAGE_SERVICE, TELEPHONY_SERVICE, TEXT_SERVICES_MANAGER_SERVICE, UI_MODE_SERVICE, USB_SERVICE, VIBRATOR_SERVICE, WALLPAPER_SERVICE, WIFI_P2P_SERVICE, WIFI_SERVICE, WINDOW_SERVICE| Constructor and Description |
|---|
WloBaseListActivity() |
| Modifier and Type | Method and Description |
|---|---|
protected abstract android.support.v4.widget.SimpleCursorAdapter |
createAdapter() |
protected M |
createNewModel(android.widget.AdapterView l,
int position) |
protected abstract M |
createNewModel(android.database.Cursor cursor) |
protected void |
editItem(int position) |
protected android.support.v4.widget.SimpleCursorAdapter.ViewBinder |
getAdapterBinder() |
protected abstract android.database.Cursor |
getAllData() |
protected Integer |
getContentView() |
protected abstract Class<? extends WloModelEditionActivity> |
getEditionActivity() |
android.widget.ListAdapter |
getListAdapter()
Get the ListAdapter associated with this activity's ListView.
|
android.widget.ListView |
getListView()
Get the activity's list view widget.
|
protected abstract Class<? extends WloBaseActivity> |
getNextActivity() |
long |
getSelectedItemId()
Get the cursor row ID of the currently selected list item.
|
int |
getSelectedItemPosition()
Get the position of the currently selected list item.
|
protected abstract Integer |
getSubtitle() |
android.support.v4.app.FragmentManager |
getSupportFragmentManager() |
android.content.Intent |
getSupportParentActivityIntent() |
protected void |
onActivityResult(int requestCode,
int resultCode,
android.content.Intent data) |
void |
onConfigurationChanged(android.content.res.Configuration newConfig) |
boolean |
onContextItemSelected(android.view.MenuItem item) |
protected void |
onCreate(android.os.Bundle savedInstanceState) |
void |
onCreateContextMenu(android.view.ContextMenu menu,
android.view.View v,
android.view.ContextMenu.ContextMenuInfo menuInfo) |
boolean |
onCreateOptionsMenu(android.view.Menu menu) |
protected void |
onDestroy() |
protected void |
onListItemClick(android.widget.ListView l,
android.view.View v,
int position,
long id)
This method will be called when an item in the list is selected.
|
boolean |
onOptionsItemSelected(android.view.MenuItem item) |
protected void |
onPostCreate(android.os.Bundle savedInstanceState) |
protected void |
onRestoreInstanceState(android.os.Bundle state)
Ensures the list view has been created before Activity restores all
of the view states.
|
protected void |
onResume() |
void |
onSupportContentChanged()
Updates the screen state (current list and other views) when the
content changes.
|
protected void |
selectItem(int position) |
protected void |
setDrawerListAdapter() |
void |
setListAdapter(android.support.v4.widget.SimpleCursorAdapter adapter)
Provide the cursor for the list view.
|
void |
setSelection(int position)
Set the currently selected list item to the specified
position with the adapter's data
|
protected void |
updateDrawerItems(M model) |
cancel, doUnbindService, getDownloadingApkId, getUpActivity, onPause, onServiceConnected, onServiceDisconnected, setDownloadingApkIdaddContentView, getDrawerToggleDelegate, getMenuInflater, getSupportActionBar, onBackPressed, onContentChanged, onCreatePanelMenu, onCreatePanelView, onCreateSupportNavigateUpTaskStack, onMenuItemSelected, onPostResume, onPrepareOptionsPanel, onPreparePanel, onPrepareSupportNavigateUpTaskStack, onStop, onSupportActionModeFinished, onSupportActionModeStarted, onSupportNavigateUp, onTitleChanged, setContentView, setContentView, setContentView, setSupportProgress, setSupportProgressBarIndeterminate, setSupportProgressBarIndeterminateVisibility, setSupportProgressBarVisibility, startSupportActionMode, supportInvalidateOptionsMenu, supportNavigateUpTo, supportRequestWindowFeature, supportShouldUpRecreateTaskdump, getLastCustomNonConfigurationInstance, getSupportFragmentManager, getSupportLoaderManager, onAttachFragment, onCreateView, onKeyDown, onLowMemory, onNewIntent, onPanelClosed, onResumeFragments, onRetainCustomNonConfigurationInstance, onRetainNonConfigurationInstance, onSaveInstanceState, onStart, startActivityForResult, startActivityFromFragmentcloseContextMenu, closeOptionsMenu, createPendingResult, dismissDialog, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, findViewById, finish, finishActivity, finishActivityFromChild, finishAffinity, finishFromChild, getActionBar, getApplication, getCallingActivity, getCallingPackage, getChangingConfigurations, getComponentName, getCurrentFocus, getFragmentManager, getIntent, getLastNonConfigurationInstance, getLayoutInflater, getLoaderManager, getLocalClassName, getParent, getParentActivityIntent, getPreferences, getRequestedOrientation, getSystemService, getTaskId, getTitle, getTitleColor, getVolumeControlStream, getWindow, getWindowManager, hasWindowFocus, invalidateOptionsMenu, isChangingConfigurations, isChild, isFinishing, isTaskRoot, managedQuery, moveTaskToBack, navigateUpTo, navigateUpToFromChild, onActionModeFinished, onActionModeStarted, onApplyThemeResource, onAttachedToWindow, onAttachFragment, onChildTitleChanged, onContextMenuClosed, onCreateDescription, onCreateDialog, onCreateDialog, onCreateNavigateUpTaskStack, onCreateThumbnail, onCreateView, onDetachedFromWindow, onGenericMotionEvent, onKeyLongPress, onKeyMultiple, onKeyShortcut, onKeyUp, onMenuOpened, onNavigateUp, onNavigateUpFromChild, onOptionsMenuClosed, onPrepareDialog, onPrepareDialog, onPrepareNavigateUpTaskStack, onPrepareOptionsMenu, onRestart, onSearchRequested, onTouchEvent, onTrackballEvent, onTrimMemory, onUserInteraction, onUserLeaveHint, onWindowAttributesChanged, onWindowFocusChanged, onWindowStartingActionMode, openContextMenu, openOptionsMenu, overridePendingTransition, recreate, registerForContextMenu, removeDialog, requestWindowFeature, runOnUiThread, setDefaultKeyMode, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setFinishOnTouchOutside, setIntent, setProgress, setProgressBarIndeterminate, setProgressBarIndeterminateVisibility, setProgressBarVisibility, setRequestedOrientation, setResult, setResult, setSecondaryProgress, setTitle, setTitle, setTitleColor, setVisible, setVolumeControlStream, shouldUpRecreateTask, showDialog, showDialog, startActionMode, startActivities, startActivities, startActivity, startActivity, startActivityForResult, startActivityFromChild, startActivityFromChild, startActivityFromFragment, startActivityFromFragment, startActivityIfNeeded, startActivityIfNeeded, startIntentSender, startIntentSender, startIntentSenderForResult, startIntentSenderForResult, startIntentSenderFromChild, startIntentSenderFromChild, startManagingCursor, startNextMatchingActivity, startNextMatchingActivity, startSearch, stopManagingCursor, takeKeyEvents, triggerSearch, unregisterForContextMenubindService, checkCallingOrSelfPermission, checkCallingOrSelfUriPermission, checkCallingPermission, checkCallingUriPermission, checkPermission, checkUriPermission, checkUriPermission, clearWallpaper, createPackageContext, databaseList, deleteDatabase, deleteFile, enforceCallingOrSelfPermission, enforceCallingOrSelfUriPermission, enforceCallingPermission, enforceCallingUriPermission, enforcePermission, enforceUriPermission, enforceUriPermission, fileList, getApplicationContext, getApplicationInfo, getAssets, getBaseContext, getCacheDir, getClassLoader, getContentResolver, getDatabasePath, getDir, getExternalCacheDir, getExternalFilesDir, getFilesDir, getFileStreamPath, getMainLooper, getObbDir, getPackageCodePath, getPackageManager, getPackageName, getPackageResourcePath, getResources, getSharedPreferences, getWallpaper, getWallpaperDesiredMinimumHeight, getWallpaperDesiredMinimumWidth, grantUriPermission, isRestricted, openFileInput, openFileOutput, openOrCreateDatabase, openOrCreateDatabase, peekWallpaper, registerReceiver, registerReceiver, removeStickyBroadcast, revokeUriPermission, sendBroadcast, sendBroadcast, sendOrderedBroadcast, sendOrderedBroadcast, sendStickyBroadcast, sendStickyOrderedBroadcast, setWallpaper, setWallpaper, startInstrumentation, startService, stopService, unbindService, unregisterReceiverpublic static final String INTENT_EXTRA_PARENT_MODEL
public static final int REQUEST_EDIT_MODEL
protected BaseModel parentModel
protected WloSqlOpenHelper woh
protected android.support.v4.widget.SimpleCursorAdapter adapter
protected android.support.v4.app.ActionBarDrawerToggle mDrawerToggle
protected android.widget.ListView mDrawerList
protected android.widget.ListView mList
protected abstract android.support.v4.widget.SimpleCursorAdapter createAdapter()
protected abstract android.database.Cursor getAllData()
protected abstract M createNewModel(android.database.Cursor cursor)
protected M createNewModel(android.widget.AdapterView l, int position)
protected abstract Class<? extends WloBaseActivity> getNextActivity()
protected abstract Class<? extends WloModelEditionActivity> getEditionActivity()
protected abstract Integer getSubtitle()
protected Integer getContentView()
getContentView in class WloBaseActivityprotected android.support.v4.widget.SimpleCursorAdapter.ViewBinder getAdapterBinder()
protected void onCreate(android.os.Bundle savedInstanceState)
onCreate in class WloBaseActivityprotected void onPostCreate(android.os.Bundle savedInstanceState)
onPostCreate in class android.app.Activityprotected void onResume()
onResume in class WloBaseActivityprotected void onDestroy()
onDestroy in class WloBaseActivitypublic android.content.Intent getSupportParentActivityIntent()
getSupportParentActivityIntent in interface android.support.v4.app.TaskStackBuilder.SupportParentablegetSupportParentActivityIntent in class WloBaseActivitypublic boolean onCreateOptionsMenu(android.view.Menu menu)
onCreateOptionsMenu in class android.app.Activitypublic boolean onOptionsItemSelected(android.view.MenuItem item)
onOptionsItemSelected in class android.app.Activitypublic void onConfigurationChanged(android.content.res.Configuration newConfig)
onConfigurationChanged in interface android.content.ComponentCallbacksonConfigurationChanged in class android.support.v7.app.ActionBarActivityprotected void onActivityResult(int requestCode,
int resultCode,
android.content.Intent data)
onActivityResult in class android.support.v4.app.FragmentActivityprotected void setDrawerListAdapter()
protected void onListItemClick(android.widget.ListView l,
android.view.View v,
int position,
long id)
l - The ListView where the click happenedv - The view that was clicked within the ListViewposition - The position of the view in the listid - The row id of the item that was clickedpublic void onCreateContextMenu(android.view.ContextMenu menu,
android.view.View v,
android.view.ContextMenu.ContextMenuInfo menuInfo)
onCreateContextMenu in interface android.view.View.OnCreateContextMenuListeneronCreateContextMenu in class android.app.Activitypublic boolean onContextItemSelected(android.view.MenuItem item)
onContextItemSelected in class android.app.Activityprotected void onRestoreInstanceState(android.os.Bundle state)
onRestoreInstanceState in class android.app.Activitypublic void onSupportContentChanged()
onSupportContentChanged in class android.support.v7.app.ActionBarActivitypublic void setListAdapter(android.support.v4.widget.SimpleCursorAdapter adapter)
public void setSelection(int position)
position - public int getSelectedItemPosition()
public long getSelectedItemId()
public android.widget.ListView getListView()
public android.widget.ListAdapter getListAdapter()
protected void updateDrawerItems(M model)
protected void selectItem(int position)
protected void editItem(int position)
public android.support.v4.app.FragmentManager getSupportFragmentManager()
Copyright © 2013–2014 CodeLutin. All rights reserved.