Events.java
上传用户:xmjingguan
上传日期:2009-07-06
资源大小:2054k
文件大小:3k
源码类别:

android开发

开发平台:

Java

  1. /***
  2.  * Excerpted from "Hello, Android!",
  3.  * published by The Pragmatic Bookshelf.
  4.  * Copyrights apply to this code. It may not be used to create training material, 
  5.  * courses, books, articles, and the like. Contact us if you are in doubt.
  6.  * We make no guarantees that this code is fit for any purpose. 
  7.  * Visit http://www.pragmaticprogrammer.com/titles/eband for more book information.
  8. ***/
  9. package org.example.events;
  10. import static android.provider.BaseColumns._ID;
  11. import static org.example.events.Constants.TABLE_NAME;
  12. import static org.example.events.Constants.TIME;
  13. import static org.example.events.Constants.TITLE;
  14. import android.app.Activity;
  15. import android.content.ContentValues;
  16. import android.database.Cursor;
  17. import android.database.sqlite.SQLiteDatabase;
  18. import android.os.Bundle;
  19. import android.widget.TextView;
  20. public class Events extends Activity {
  21.    
  22.    
  23.    private static String[] FROM = { _ID, TIME, TITLE, };
  24.    private static String ORDER_BY = TIME + " DESC";
  25.    
  26.    
  27.    private EventsData events;
  28.    @Override
  29.    public void onCreate(Bundle savedInstanceState) {
  30.       super.onCreate(savedInstanceState);
  31.       setContentView(R.layout.main); 
  32.       events = new EventsData(this); 
  33.       try {
  34.          addEvent("Hello, Android!"); 
  35.          Cursor cursor = getEvents(); 
  36.          showEvents(cursor); 
  37.       } finally {
  38.          events.close(); 
  39.       }
  40.    }
  41.    
  42.    
  43.    private void addEvent(String string) {
  44.       // Insert a new record into the Events data source.
  45.       // You would do something similar for delete and update.
  46.       SQLiteDatabase db = events.getWritableDatabase();
  47.       ContentValues values = new ContentValues();
  48.       values.put(TIME, System.currentTimeMillis());
  49.       values.put(TITLE, string);
  50.       db.insertOrThrow(TABLE_NAME, null, values);
  51.    }
  52.    
  53.    
  54.    private Cursor getEvents() {
  55.       // Perform a managed query. The Activity will handle closing
  56.       // and re-querying the cursor when needed.
  57.       SQLiteDatabase db = events.getReadableDatabase();
  58.       Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
  59.             null, ORDER_BY);
  60.       startManagingCursor(cursor);
  61.       return cursor;
  62.    }
  63.    
  64.    
  65.    private void showEvents(Cursor cursor) {
  66.       // Stuff them all into a big string
  67.       StringBuilder builder = new StringBuilder( 
  68.             "Saved events:n");
  69.       while (cursor.moveToNext()) { 
  70.          // Could use getColumnIndexOrThrow() to get indexes
  71.          long id = cursor.getLong(0); 
  72.          long time = cursor.getLong(1);
  73.          String title = cursor.getString(2);
  74.          builder.append(id).append(": "); 
  75.          builder.append(time).append(": ");
  76.          builder.append(title).append("n");
  77.       }
  78.       // Display on the screen
  79.       TextView text = (TextView) findViewById(R.id.text); 
  80.       text.setText(builder);
  81.    }
  82.    
  83.    
  84. }