Events类
package org.examples.events;
import android.app.ListActivity;
import android.content.ContentValues;
import android.widget.SimpleCursorAdapter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;
import static org.examples.events.Constants.TABLE_NAME;
import static org.examples.events.Constants.TITLE;
import static org.examples.events.Constants.TIME;
import static android.provider.BaseColumns._ID;
public class Events extends ListActivity {
private EventsData events;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
events = new EventsData(this);
try {
addEvent("Hello Tarun");
Cursor cursor = getEvent();
showEvent(cursor);
} finally {
events.close();
}
}
// Insert data into the database
// similar things can be done for delete or update
private void addEvent(String string) {
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(TIME, System.currentTimeMillis());
values.put(TITLE, string);
db.insertOrThrow(TABLE_NAME, null, values);
}
// GET THE LIST OF EVENTS FROM DATABASE
private static String[] FROM = { _ID, TIME, TITLE };
private static String ORDER_BY = TIME + "DESC";
private Cursor getEvent() {
// PERFORM A MANAGED QUERY. THE ACTIVITY WILL HANDLE CLOSING AND
// REQUERING THE DATABASE WHEN NEEDED
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * from " +TABLE_NAME, null);
//Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null, null, ORDER_BY);
//Cursor c = sampleDB.rawQuery("SELECT * FROM " +TABLE_NAME, null);
startManagingCursor(cursor);
return cursor;
}
// STUFF THEM ALL INTO A BIG STRINGS
// THE ADAPTER ACTS AS THE GO BETWEEN THE DATA SOURCE AND THE VIEW
public static int[] TO = { R.id.rowid, R.id.time, R.id.title };
private void showEvent(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
}
EventsData类为
package org.examples.events;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static org.examples.events.Constants.TABLE_NAME;
import static org.examples.events.Constants.TITLE;
import static org.examples.events.Constants.TIME;
import static android.provider.BaseColumns._ID;
public class EventsData extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "events.db";
public static final int DATABASE_VERSION = 1;
public static final String CREATE_TABLE = "CREATE TABLE events (" +Constants._ID+
" INT AUTO_INCREMENT PRIMARY KEY," +
" time INTEGER, title TEXT NOT NULL); ";
public EventsData(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists" + TABLE_NAME);
onCreate(db);
}
}
和常数如下
package org.examples.events;
import android.provider.BaseColumns;;
public interface Constants extends BaseColumns{
public static final String TABLE_NAME = " events ";
// coloumns in the table
public static final String TIME = " time ";
public static final String TITLE = " title ";
public static final String _ID = " _id ";
}
答案 0 :(得分:0)
,您有一个Events
import
而不是被覆盖的
android.provider.BaseColumns._ID;