TestDatabase.java (Cont.)

public void onClick( View view ) {
The main.xml specifies when a button is clicked, this method will be activated:
       android:onClick="onClick"
@SuppressWarnings( "unchecked" )
The SuppressWarning annotation is used to suppress compiler warnings for the annotated element. Specifically, the unchecked category allows suppression of compiler warnings generated as a result of unchecked type casts. It essentially implies that the programmer does not wish to be notified about these which he/she is already aware of the problems.

ArrayAdapter<Comment> adapter =
      ( ArrayAdapter ) getListAdapter( );
adapter.add( comment );
adapter.remove( comment );
The getListAdapter method gets the ListAdapter associated with this activity’s ListView in the layout, R.layout.main, with the attribute:
     android:id="@android:id/list"
ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items:

  • add, which adds the specified object at the end of the array and
  • remove, which removes the specified object from the array.

Comment comment = null;
The class Comment contains the data we will save in the database and show in the user interface.
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( );
  }
}