TestDatabase.java (Cont.)

int id = view.getId( );
if ( id == R.id.add ) {
if ( id == R.id.delete ) {
The IDs add and delete are defined in the file main.xml.

String[ ] comments = new String[ ]
      { "Cool", "Very nice", "Hate it" };
Create a string array comments with three elements “Cool,” “Very nice,” and “Hate it.”

int nextInt = new Random( ).nextInt( 3 );
The method nextInt(int n) returns a pseudo-random uniformly distributed integer in the half-open range [0, n).

comment = datasource.createComment(
      comments[nextInt] );
The class CommentsDataSource is this application’s DAO (data access object), which provides an abstract interface to a database. The method createComment, which adds a new comment to the database and returns the comment, is implemented as follows:
  public Comment createComment( String comment ) {
    ContentValues values = new ContentValues( );
    values.put( MySQLiteHelper.COLUMN_COMMENT, comment );
    long insertId = database.insert(
      MySQLiteHelper.TABLE_COMMENTS, null, values );
    Cursor cursor = database.query( MySQLiteHelper.TABLE_COMMENTS,
      allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId,
      null, null, null, null );
    cursor.moveToFirst( );
    return cursorToComment( cursor );
  }
adapter.add( comment );
Add the specified object at the end of the array.
src/main/java/com/example/wenchen/sqlitedemo/TestDatabase.java
01package com.example.wenchen.sqlitedemo;
02 
03import java.util.List;
04import java.util.Random;
05import android.app.ListActivity;
06import android.os.Bundle;
07import android.view.View;
08import android.widget.ArrayAdapter;
09 
10public class TestDatabase extends ListActivity {
11  private CommentsDataSource datasource;
12 
13  @Override
14  public void onCreate( Bundle savedInstanceState ) {
15    super.onCreate( savedInstanceState );
16    setContentView( R.layout.main );
17 
18    datasource = new CommentsDataSource( this );
19    datasource.open( );
20 
21    List<Comment> values = datasource.getAllComments( );
22 
23    // Use the SimpleCursorAdapter to show the elements in a ListView.
24    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(
25      this, android.R.layout.simple_list_item_1, values );
26    setListAdapter( adapter );
27  }
28 
29  // Will be called via the onClick attribute of the buttons in main.xml.
30  public void onClick( View view ) {
31    @SuppressWarnings( "unchecked" )
32    ArrayAdapter<Comment> adapter = ( ArrayAdapter<Comment> ) getListAdapter( );
33    Comment comment = null;
34    int id = view.getId( );
35    if ( id == R.id.add ) {
36      String[ ] comments = new String[ ] { "Cool", "Very nice", "Hate it" };
37      int nextInt = new Random( ).nextInt( 3 );
38      // Save the new comment to the database.
39      comment = datasource.createComment( comments[nextInt] );
40      adapter.add( comment );
41    }
42    else if ( id == R.id.delete ) {
43      if ( getListAdapter( ).getCount( ) > 0 ) {
44        comment = (Comment) getListAdapter( ).getItem( 0 );
45        datasource.deleteComment( comment );
46        adapter.remove( comment );
47      }
48    }
49    adapter.notifyDataSetChanged( );
50  }
51 
52  @Override
53  protected void onResume( ) {
54    datasource.open( );
55    super.onResume( );
56  }
57 
58  @Override
59  protected void onPause( ) {
60    datasource.close( );
61    super.onPause( );
62  }
63}