Database backed Content provider
Description
The following code shows how to create Database backed Content provider.
Example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/listView"/>
</LinearLayout>
Java code
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
/*ww w. java2 s . c o m*/
public class MainActivity extends Activity {
private MyDatabaseHelper mDatabaseHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
mDatabaseHelper = new MyDatabaseHelper(this);
Cursor c = mDatabaseHelper.query(MyDatabaseHelper.TABLE_USERS, MyDatabaseHelper.COL_NAME);
String[] from = new String[]{MyDatabaseHelper.COL_NAME, MyDatabaseHelper.COL_EMAIL};
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, from, to, 0);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
addUser(null, null, 0);
addUser("XML", "xml@example.com", 0);
addUser("HRML", "html@example.com", 0);
addUser("CSS", "css@example.com", 0);
}
private void addUser(String name, String email, long dateOfBirthMillis) {
ContentValues values = new ContentValues();
values.put(MyDatabaseHelper.COL_NAME, name);
if (email != null) {
values.put(MyDatabaseHelper.COL_EMAIL, email);
}
if (dateOfBirthMillis != 0) {
values.put(MyDatabaseHelper.COL_DOB, dateOfBirthMillis);
}
try {
mDatabaseHelper.insert(MyDatabaseHelper.TABLE_USERS, values);
} catch (MyDatabaseHelper.NotValidException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String TABLE_USERS = "users";
public static final String COL_ID = BaseColumns._ID;
public static final String COL_NAME = "name";
public static final String COL_EMAIL = "email";
public static final String COL_DOB = "date_of_birth";
private static final String DATABASE_NAME = "my_app.db";
private static final int DATABASE_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_USERS + " ("
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COL_NAME + " TEXT NOT NULL,"
+ COL_EMAIL + " TEXT,"
+ COL_DOB + " INTEGER"
+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS + ";");
onCreate(db);
}
public long insert(String tableName, ContentValues values) throws NotValidException {
validate(values);
return getWritableDatabase().insert(tableName, null, values);
}
public int update(String tableName, long id, ContentValues values) throws NotValidException {
validate(values);
String selection = COL_ID + " = ?";
String[] selectionArgs = {String.valueOf(id)};
return getWritableDatabase().update(tableName, values, selection, selectionArgs);
}
public int delete(String tableName, long id) {
String selection = COL_ID + " = ?";
String[] selectionArgs = {String.valueOf(id)};
return getWritableDatabase().delete(tableName, selection, selectionArgs);
}
protected void validate(ContentValues values) throws NotValidException {
if (!values.containsKey(COL_NAME) || values.getAsString(COL_NAME) == null || values.getAsString(COL_NAME).isEmpty()) {
throw new NotValidException("User name must be set");
}
}
public Cursor query(String tableName, String orderedBy) {
String[] projection = {COL_ID, COL_NAME, COL_EMAIL, COL_DOB};
return getReadableDatabase().query(tableName, projection, null, null, null, null, orderedBy);
}
public static class NotValidException extends Throwable {
public NotValidException(String msg) {
super(msg);
}
}
}
The code above generates the following result.