/*** Copyright (c) 2011-2012 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. From _The Busy Coder's Guide to Advanced Android Development_ http://commonsware.com/AdvAndroid */ package com.commonsware.android.cal.query; import android.app.ListActivity; import android.app.LoaderManager; import android.content.CursorLoader; import android.content.Loader; import android.database.Cursor; import android.os.Bundle; import android.provider.CalendarContract; import android.text.format.DateUtils; import android.view.View; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class CalendarQueryActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor>, SimpleCursorAdapter.ViewBinder { private static final String[] PROJECTION= new String[] { CalendarContract.Events._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND }; private static final String[] ROW_COLUMNS= new String[] { CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND }; private static final int[] ROW_IDS= new int[] { R.id.title, R.id.dtstart, R.id.dtend }; private SimpleCursorAdapter adapter=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); adapter= new SimpleCursorAdapter(this, R.layout.row, null, ROW_COLUMNS, ROW_IDS); adapter.setViewBinder(this); setListAdapter(adapter); getLoaderManager().initLoader(0, null, this); } public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) { return(new CursorLoader(this, CalendarContract.Events.CONTENT_URI, PROJECTION, null, null, CalendarContract.Events.DTSTART)); } public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { adapter.swapCursor(cursor); } public void onLoaderReset(Loader<Cursor> loader) { adapter.swapCursor(null); } @Override public boolean setViewValue(View view, Cursor cursor, int columnIndex) { long time=0; String formattedTime=null; switch (columnIndex) { case 2: case 3: time=cursor.getLong(columnIndex); formattedTime= DateUtils.formatDateTime(this, time, DateUtils.FORMAT_ABBREV_RELATIVE); ((TextView)view).setText(formattedTime); break; default: return(false); } return(true); } }