TestDatabase.java (Cont.)

if ( getListAdapter( ).getCount( ) > 0 ) {
The getCount method returns the number of items in the data set represented by this Adapter.

comment = (Comment) getListAdapter( ).getItem( 0 );
The method getItem(int position) get the data item associated with the specified position in the data set.

datasource.deleteComment( comment );
The method deleteComment, which deletes the comment from the database, is implemented as follows:
  public void deleteComment( Comment comment ) {
    long id = comment.getId( );
    System.out.println( "Comment deleted with id: " + id );
    database.delete( MySQLiteHelper.TABLE_COMMENTS,
      MySQLiteHelper.COLUMN_ID + " = " + id, null );
  }
adapter.remove( comment );
Removes the specified object from the array.

adapter.notifyDataSetChanged( );
Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

protected void onResume( ) {
It is called when the activity is moved to the foreground of the visible space, when the user is able to not only see the activity but interact with it.

protected void onPause( ) {
It is called when the activity is no longer in the foreground, usually as the result of another activity being started.
src/main/java/com/example/wenchen/sqlitedemo/TestDatabase.java
package com.example.wenchen.sqlitedemo;

import java.util.List;
import java.util.Random;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class TestDatabase extends ListActivity {
  private CommentsDataSource datasource;

  @Override
  public void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.main );

    datasource = new CommentsDataSource( this );
    datasource.open( );

    List<Comment> values = datasource.getAllComments( );

    // Use the SimpleCursorAdapter to show the elements in a ListView.
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(
      this, android.R.layout.simple_list_item_1, values );
    setListAdapter( adapter );
  }

  // Will be called via the onClick attribute of the buttons in main.xml.
  public void onClick( View view ) {
    @SuppressWarnings( "unchecked" )
    ArrayAdapter<Comment> adapter = ( ArrayAdapter<Comment> ) getListAdapter( );
    Comment comment = null;
    int id = view.getId( );
    if ( id == R.id.add ) {
      String[ ] comments = new String[ ] { "Cool", "Very nice", "Hate it" };
      int nextInt = new Random( ).nextInt( 3 );
      // Save the new comment to the database.
      comment = datasource.createComment( comments[nextInt] );
      adapter.add( comment );
    }
    else if ( id == R.id.delete ) {
      if ( getListAdapter( ).getCount( ) > 0 ) {
        comment = (Comment) getListAdapter( ).getItem( 0 );
        datasource.deleteComment( comment );
        adapter.remove( comment );
      }
    }
    adapter.notifyDataSetChanged( );
  }

  @Override
  protected void onResume( ) {
    datasource.open( );
    super.onResume( );
  }

  @Override
  protected void onPause( ) {
    datasource.close( );
    super.onPause( );
  }
}




      Two hunters are out in the woods when one of them collapses.    
      He’s not breathing and his eyes are glazed.    
      The other guy whips out his cell phone and calls 911.    
      “I think my friend is dead!” he yells. “What can I do?”    
      The operator says, “Calm down. First, let’s make sure he’s dead.”    
      There’s a silence, then a shot.    
      Back on the phone, the guy says, “OK, now what?”