我在我的應用程序中有一個內容提供商,我真的不喜歡它:-)。提供者是在閱讀了the official one和this unofficial one之類的文章之後創建的。更好的方式來創建Android內容提供商
我真的不喜歡的是提供程序類中的重複量(請參閱下面的非常精簡的代碼)。即使對於兩個(或四個,這取決於你如何計數)內容類型,也有太多的代碼。
我想過要去其他路線:在內容提供商的原始SQL查詢,但不知道這是否會變得更加優雅。
請指教一個更好的方式來創建Android內容提供者,然後在下面的代碼中創建。
public abstract class BaseProvider extends ContentProvider
{
protected DatabaseHelper m_helper;
protected static class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "my.db";
private static final int DATABASE_VERSION = 4;
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
FolderTable.onCreate(db);
TrackTable.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
FolderTable.onUpgrade(db, oldVersion, newVersion);
TrackTable.onUpgrade(db, oldVersion, newVersion);
}
}
@Override
public boolean onCreate()
{
m_helper = new DatabaseHelper(getContext());
return true;
}
// ...
}
public class MyProvider extends BaseProvider
{
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int FOLDERS = 10;
private static final int FOLDER_ID = 20;
private static final int TRACKS = 30;
private static final int TRACK_ID = 40;
private static final String FOLDERS_BASE_PATH = "folders";
private static final String TRACKS_BASE_PATH = "tracks";
public static final String AUTHORITY = ".contentprovider.MyProvider";
private static final String CONTENT_URI_BASE = "content://" + AUTHORITY + "/";
public static final Uri FOLDERS_CONTENT_URI = Uri.parse(CONTENT_URI_BASE + FOLDERS_BASE_PATH);
public static final Uri TRACKS_CONTENT_URI = Uri.parse(CONTENT_URI_BASE + TRACKS_BASE_PATH);
static
{
sURIMatcher.addURI(AUTHORITY, FOLDERS_BASE_PATH, FOLDERS);
sURIMatcher.addURI(AUTHORITY, FOLDERS_BASE_PATH + "/#", FOLDER_ID);
sURIMatcher.addURI(AUTHORITY, TRACKS_BASE_PATH, TRACKS);
sURIMatcher.addURI(AUTHORITY, TRACKS_BASE_PATH + "/#", TRACK_ID);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
{
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
int uriType = sURIMatcher.match(uri);
switch (uriType)
{
case FOLDERS:
queryBuilder.setTables(getTablesForFolders());
break;
case FOLDER_ID:
queryBuilder.setTables(FolderTable.TABLE_NAME);
queryBuilder.appendWhere(FolderTable.COLUMN_ID + "=" + uri.getLastPathSegment());
break;
case TRACKS:
queryBuilder.setTables(getTablesForTracks());
break;
case TRACK_ID:
queryBuilder.setTables(TrackTable.TABLE_NAME);
queryBuilder.appendWhere(TrackTable.COLUMN_ID + "=" + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase db = m_helper.getWritableDatabase();
if (db == null)
return null;
Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
if (cursor == null)
return null;
setNotificationUri(cursor, uri);
return cursor;
}
// ....
}
你在說什麼重複?你能更具體一點,並強調代碼中的區域? – kpsfoo