Next is the layout for the main app:

<?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"
    >
    <TextView
        android:id="@+id/timeTextView"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Alarmsutta!"
        android:textSize="40px"
        android:gravity="center"
        android:layout_marginTop="20px"
        android:layout_marginBottom="20px"
    />
        <ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/alarmsListView"
    />
</LinearLayout>


On Jul 23, 6:20 pm, eags <[email protected]> wrote:
> Ok, Just for the practice, I'm implementing something very similar to
> the Alarm Clock app included in Android by default.  I'm going to put
> each file in their own post.
>
> First in this post, Alarmsutta.java which includes the main setup, and
> the CursorAdapter:
>
> package net.esalazar.alarmsutta;
>
> import java.text.SimpleDateFormat;
> import java.util.Calendar;
> import java.util.Date;
>
> import android.app.Activity;
> import android.content.Context;
> import android.content.Intent;
> import android.content.res.Resources;
> import android.database.Cursor;
> import android.os.Bundle;
> import android.view.LayoutInflater;
> import android.view.Menu;
> import android.view.MenuItem;
> import android.view.View;
> import android.view.ViewGroup;
> import android.view.View.OnClickListener;
> import android.widget.AdapterView;
> import android.widget.CheckBox;
> import android.widget.CompoundButton;
> import android.widget.CursorAdapter;
> import android.widget.LinearLayout;
> import android.widget.ListView;
> import android.widget.TextView;
> import android.widget.AdapterView.OnItemSelectedListener;
> import android.widget.CompoundButton.OnCheckedChangeListener;
>
> public class Alarmsutta extends Activity {
>
>         private Alarm selectedAlarm;
>         private AlarmDBAdapter dbAdapter;
>
>         // Options menu codes
>         public final int MENU_NEW = Menu.FIRST;
>         public final int MENU_EDIT = Menu.FIRST + 1;
>         public final int MENU_DELETE = Menu.FIRST + 2;
>
>         // Intent codes for starting activities
>         public final int NEW_ALARM = 1;
>         public final int EDIT_ALARM = 2;
>
>         ListView alarmsListView;
>         AlarmCursorAdapter cursorAdapter;
>
>     Cursor allAlarmsCursor;
>
>     private LayoutInflater layoutFactory;
>
>     private class AlarmCursorAdapter extends CursorAdapter {
>
>                 public AlarmCursorAdapter(Context context, Cursor c) {
>                         super(context, c, true);
>                 }
>
>                 @Override
>                 public View newView(Context context, Cursor cursor, ViewGroup
> parent) {
>                         return layoutFactory.inflate(R.layout.alarmlist_item, 
> parent,
> false);
>                 }
>
>                 @Override
>                 public void bindView(View view, Context context, Cursor 
> cursor) {
>                         final Alarm item = 
> dbAdapter.getAlarmFromCursor(cursor);
>                         LinearLayout alarmView = (LinearLayout)view;
>
>                         Date expiry = item.getExpiry();
>                         String title = item.getTitle();
>
>                         SimpleDateFormat sdf;
>                         sdf = new SimpleDateFormat("MM/dd/yyyy");
>                         String expiryDateString = sdf.format(expiry);
>                         sdf = new SimpleDateFormat("hh:mm");
>                         String expiryTimeString = sdf.format(expiry);
>
>                         String message = item.getMessage();
>                         final boolean enabled = item.isEnabled();
>
>                         Calendar calendar = Calendar.getInstance();
>                         calendar.setTime(expiry);
>
>                         // Get references to all the elements we wish to muck 
> with
>                         TextView titleTextView = 
> (TextView)alarmView.findViewById
> (R.id.itemTitleTextView);
>                         TextView messageTextView = 
> (TextView)alarmView.findViewById
> (R.id.itemMessageTextView);
>                         TextView timeTextView = 
> (TextView)alarmView.findViewById
> (R.id.itemTimeTextView);
>
>                         // Dim the AM/PM label accordingly leaving the 
> correct one bright
>                         TextView amTextView = (TextView)alarmView.findViewById
> (R.id.itemAMTextView);
>                         TextView pmTextView = (TextView)alarmView.findViewById
> (R.id.itemPMTextView);
>                         Resources r = getResources();
>                         int amPmColorOn = r.getColor(R.color.ampm_on);
>                         int amPmColorOff = r.getColor(R.color.ampm_off);
>                         if( calendar.get(Calendar.HOUR_OF_DAY) >= 12 ) {
>                                 amTextView.setTextColor(amPmColorOff);
>                                 pmTextView.setTextColor(amPmColorOn);
>                         } else {
>                                 amTextView.setTextColor(amPmColorOn);
>                                 pmTextView.setTextColor(amPmColorOff);
>                         }
>                         CheckBox enabledCheckBox = 
> (CheckBox)alarmView.findViewById
> (R.id.itemEnabledCheckBox);
>
>                         // Set the title and message accordingly.  We prefix 
> the message
> with
>                         // the date just to be coy.
>                         titleTextView.setText(title);
>                         messageTextView.setText( expiryDateString + "  " + 
> message );
>                         timeTextView.setText(expiryTimeString);
>                         enabledCheckBox.setChecked(enabled);
>
>                         enabledCheckBox.setOnCheckedChangeListener(new
> OnCheckedChangeListener() {
>                                 public void onCheckedChanged(CompoundButton 
> buttonView, boolean
> isChecked) {
>                                         item.setEnabled(isChecked);
>                                         dbAdapter.updateAlarm(item.getId(), 
> item);
>                                         notifyDataSetChanged();
>                                 }
>                         });
>                         LinearLayout alarmSelectionBox = (LinearLayout)
> alarmView.findViewById(R.id.alarmSelectionBox);
>                         alarmSelectionBox.setOnClickListener(new 
> OnClickListener() {
>                                 public void onClick(View v) {
>                                         
> startEditAlarm(dbAdapter.getAlarm(item.getId()));
>                                 }
>                         });
>
>                 }
>
>     }
>
>     @Override
>     public void onCreate(Bundle savedInstanceState) {
>         super.onCreate(savedInstanceState);
>         setContentView(R.layout.main);
>         layoutFactory = LayoutInflater.from(this);
>
>         // Set up array adapter and viewer for alarms list
>         dbAdapter = new AlarmDBAdapter(this);
>         dbAdapter.open();
>         allAlarmsCursor = dbAdapter.getAllAlarmItemsCursor();
>         cursorAdapter = new AlarmCursorAdapter(this, allAlarmsCursor);
>
>         alarmsListView = (ListView)findViewById(R.id.alarmsListView);
>
>         alarmsListView.setAdapter(cursorAdapter);
>         alarmsListView.setVerticalScrollBarEnabled(true);
>         alarmsListView.setItemsCanFocus(true);
>         alarmsListView.setOnItemSelectedListener(new
> OnItemSelectedListener() {
>                         public void onItemSelected(AdapterView<?> parent, 
> View view, int
> position, long id) {
>                                 selectedAlarm = dbAdapter.getAlarm(id);
>                         }
>                         public void onNothingSelected(AdapterView<?> arg0) {
>                                 selectedAlarm = null;
>                         }
>                 });
>
>     }
>
>         @Override
>         protected void onActivityResult(int requestCode, int resultCode,
> Intent data) {
>
>                 super.onActivityResult(requestCode, resultCode, data);
>
>                 if( resultCode != Activity.RESULT_OK )
>                         return;
>
>                 Date expiry = new Date(data.getLongExtra("expiryTime", 0));
>                 String title = data.getStringExtra("title");
>                 String message = data.getStringExtra("message");
>                 boolean enabled = data.getBooleanExtra("enabled", true);
>                 int id = data.getIntExtra("id", -1);
>
>                 Alarm newAlarm = new Alarm(expiry, title, message, enabled, 
> id);
>
>                 switch( requestCode ) {
>                         case( NEW_ALARM ): {
>                                 dbAdapter.insertAlarm(newAlarm);
>
>                                 break;
>                         }
>                         case( EDIT_ALARM ): {
>                                 // If we only get the default value of -1 
> then something is wrong
>                                 if( id == -1 )
>                                         throw new RuntimeException();
>
>                                 dbAdapter.updateAlarm(id, newAlarm);
>                                 break;
>                         }
>                 }
>                 allAlarmsCursor.requery();
>                 cursorAdapter.notifyDataSetChanged();
>
>         }
>
>         @Override
>         public void onDestroy() {
>                 dbAdapter.close();
>                 super.onDestroy();
>         }
>
>         @Override
>         public boolean onCreateOptionsMenu(Menu menu) {
>
>                 super.onCreateOptionsMenu(menu);
>
>                 // Add menu item for creating a new alarm and
>                 // one for deleting selected alarm
>                 menu.add(0, MENU_NEW, Menu.NONE, R.string.menu_new );
>                 menu.add(0, MENU_EDIT, Menu.NONE, R.string.menu_edit );
>                 menu.add(0, MENU_DELETE, Menu.NONE, R.string.menu_delete );
>
>                 return true;
>
>         }
>
>         @Override
>         public boolean onPrepareOptionsMenu(Menu menu) {
>                 super.onPrepareOptionsMenu(menu);
>
>                 MenuItem editItem = menu.findItem(MENU_EDIT);
>                 MenuItem deleteItem = menu.findItem(MENU_DELETE);
>
>                 boolean showEditOptions = (selectedAlarm != null);
>
>                 editItem.setVisible(showEditOptions);
>                 deleteItem.setVisible(showEditOptions);
>
>                 return true;
>         }
>
>         private void startEditAlarm(Alarm alarm) {
>                 // Tell the NewAlarm activity what to expect
>                 Intent i = new Intent(this,NewAlarm.class);
>
>                 i.putExtra("expiryTime", alarm.getExpiry().getTime());
>                 i.putExtra("title", alarm.getTitle());
>                 i.putExtra("message", alarm.getMessage());
>                 i.putExtra("enabled", alarm.isEnabled());
>                 i.putExtra("id", alarm.getId());
>
>                 startActivityForResult(i, EDIT_ALARM);
>         }
>
>         @Override
>         public boolean onOptionsItemSelected(MenuItem item) {
>
>                 // Figure out which item this is and act accordingly
>                 // If it is to create or edit
>                 switch( item.getItemId() ) {
>
>                         case( MENU_NEW ): {
>                         Intent i = new Intent(this,NewAlarm.class);
>                         startActivityForResult(i, NEW_ALARM);
>                                 return true;
>                         }
>                         case( MENU_EDIT ): {
>                                 startEditAlarm(selectedAlarm);
>                                 return true;
>                         }
>                         case( MENU_DELETE ): {
>                                 dbAdapter.removeAlarm(selectedAlarm.getId());
>                                 allAlarmsCursor.requery();
>                                 cursorAdapter.notifyDataSetChanged();
>                                 return true;
>                         }
>                 }
>
>                 return false;
>
>         }
>
> }
>
> On Jul 23, 6:16 pm, Gregg Reno <[email protected]> wrote:
>
>
>
> > It would be great if you could post the code and xml!  I'm using a
> > SimpleCursorAdapter and would love to see how you are using the
> > CursorAdaptor instead.  My app is a little tricky because I extend
> > TabActivity rather than ListActivity.  If I get it working, maybe I'll
> > put together a standalone tutorial.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to