我似乎无法理解内容提供者的概念。我个人认为它会使代码膨胀,使代码过于复杂,而且简直糟透了。
顺便说一句,这将是您在网络上找到的多表ContentProvider的第一个示例代码。绝对没有其他,除了ContactsProvider(“ veeerrrrry有用的”-被超级填充和难以理解)。我相信很多像我这样的菜鸟都会对此表示赞赏。
请帮助我从内容提供商处简单地进行游标。我有下面的NORMAL代码(使用简单的SQLiteOpenHelper),并且需要将其转换为内容提供者代码:
Button btnp = (Button) findViewById(R.id.btnproc);
btnp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Context mycontext = getApplicationContext();
SQLiteDatabase db = new myDbHelper(mycontext).getWritableDatabase();
String sql = "select _id,data,titlu,cont from notificari where date(data)<=date(datetime())";
Cursor cursor = db.rawQuery(sql, null);
Log.w("...MainActivity...", "Record count:"+Integer.toString(curu.getCount()));
while(curu.moveToNext()){
String lista = curu.getString(0)+",'"+curu.getString(1)+"',"+curu.getString(2)+",'"+
curu.getString(3)+"'";
Log.e("...MainActivity...", lista);
}
}
});
因此,我需要从content-provider获取光标。我已经开始开发这个“令人赞叹的”内容提供者,但是我不知道我在做什么是否可以,因为它太复杂了。它就来了(我的内容提供者需要引用多个表):
public class MyFirstProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.mystuff.myapp.MyFirstProvider";
static final String URL = "content://" + PROVIDER_NAME + "/cte";
static final String URL2 = "content://" + PROVIDER_NAME + "/clienti";
static final String URL3 = "content://" + PROVIDER_NAME + "/uzeri";
static final String URL4 = "content://" + PROVIDER_NAME + "/notificari";
static final Uri CONTENT_URI = Uri.parse(URL);
static final Uri CONTENT_URI2 = Uri.parse(URL2);
static final Uri CONTENT_URI3 = Uri.parse(URL3);
static final Uri CONTENT_URI4 = Uri.parse(URL4);
static final int uriCode = 1;
static final int uriCode1 = 2;
static final int uriCode2 = 3;
static final int uriCode3 = 4;
static final int uriCode4 = 5;
static final int uriCode5 = 6;
static final int uriCode6 = 7;
static final int uriCode7 = 8;
static final UriMatcher uriMatcher;
private static HashMap<String, String> values;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "cte", uriCode);
uriMatcher.addURI(PROVIDER_NAME, "cte/*", uriCode1);
uriMatcher.addURI(PROVIDER_NAME, "clienti", uriCode2);
uriMatcher.addURI(PROVIDER_NAME, "clienti/*", uriCode3);
uriMatcher.addURI(PROVIDER_NAME, "uzeri", uriCode4);
uriMatcher.addURI(PROVIDER_NAME, "uzeri/*", uriCode5);
uriMatcher.addURI(PROVIDER_NAME, "notificari", uriCode6);
uriMatcher.addURI(PROVIDER_NAME, "notificari/*", uriCode7);
}
@Override
public boolean onCreate() {
Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
if (db != null) {
return true;
}
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
switch (uriMatcher.match(uri)) {
case uriCode:
qb.setTables(TABLE_NAME);
qb.setProjectionMap(values);
break;
case uriCode2:
qb.setTables(TABLE_CLIENTI);
qb.setProjectionMap(values);
if (sortOrder == null || sortOrder == "") {
sortOrder = TCL_NUME_CLIENT;
}
break;
case uriCode6:
qb.setTables(TABLE_NOTIF);
qb.setProjectionMap(values);
break;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
Cursor c = qb.query(db, projection, selection, selectionArgs, null,null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
// ... there is other code here regarding the insert,update and delete (not relevant now)
private SQLiteDatabase db;
static final String DATABASE_NAME = "mydb";
static final String TABLE_NAME = "names";
public static final String TABLE_UZERI = "uzeri";
public static final String TABLE_NOTIF = "notificari";
static final int DATABASE_VERSION = 3;
static final String CREATE_DB_TABLE = " CREATE TABLE " + TABLE_NAME
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ " name TEXT NOT NULL);";
static final String CREATE_DB_TABLE_NOTIFICARI = "CREATE TABLE IF NOT EXISTS " + TABLE_NOTIF + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
TNOT_ID_NOT + " INTEGER NOT NULL DEFAULT 0, " +
TNOT_DATA + " DATE NOT NULL, " +
TNOT_SURSA + " INTEGER NOT NULL DEFAULT 0, "+
TNOT_DEST + " INTEGER NOT NULL DEFAULT 0, "+
TNOT_TIP + " TEXT, "+
TNOT_TITLU + " TEXT, "+
TNOT_TITLUCONT + " TEXT, "+
TNOT_CONT + " TEXT, "+
TNOT_STERS + " INTEGER DEFAULT 0, "+
TNOT_NOU + " INTEGER NOT NULL DEFAULT 0)";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE);
db.execSQL(CREATE_DB_TABLE_UZERI);
db.execSQL(CREATE_DB_TABLE_NOTIFICARI);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_UZERI);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NOTIFICARI);
onCreate(db);
}
}
我试图“翻译”记录光标内容的活动代码,但没有。非常令人沮丧。
这是我的“翻译”:
Button btnp = (Button) findViewById(R.id.btnproc);
btnp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] coloane = {MyFirstProvider.TNOT_ID_ID, MyFirstProvider.TNOT_DATA,MyFirstProvider.TNOT_TITLU, MyFirstProvider.TNOT_CONT};
Cursor curu = MainActivity.this.getContentResolver().query(MyFirstProvider.CONTENT_URI_NOTIF,coloane,null,null,MyFirstProvider.TNOT_DATA);
Log.w("...MainActivity...", "Recorduri:"+Integer.toString(curu.getCount()));
while(curu.moveToNext()){
String lista = curu.getString(0)+",'"+curu.getString(1)+"',"+curu.getString(2)+",'"+
curu.getString(3)+"'";
Log.e("...MainActivity...", lista);
}
}
});
我必须向您保证表中有4条记录(我在创建表后手动插入它们)
另外,如果我将这段代码放在ContentProvider中,则日志会根据需要显示表的内容:
Cursor c = qb.query(db, projection, selection, selectionArgs, null,null, sortOrder);
while(c.moveToNext()){
Log.w("...Provider...",c.getString(0)+"-"+c.getString(1)+"-"+c.getString(2));
}
但是我不想在内容提供程序中执行此操作,而是希望在活动代码中执行此操作。
我究竟做错了什么?
请帮忙。
谢谢
最佳答案
不必使用ContentProvider
。确定您的要求。如果数据是应用程序专用的,并且必须以表格格式存储,那么您可以直接在Android中访问SQLite引擎,而无需通过ContentProvider
进行操作。但是,如果您希望其他应用程序或系统访问您的应用程序数据,则可以-这是ContentProvider
的合法用例。
但是,我建议您阅读此article-告诉您如何使用和不使用SQLite
来访问ContentProvider
引擎。
希望这可以帮助。
无论如何,对于您眼前的问题,请尝试以下操作-
Cursor c = qb.query(db, projection, selection, selectionArgs, null,null, sortOrder);
if (c == null) {
// error - log some message
}
else if (c.getCount() < 1) {
// nothing to show - log some message
}
else {
while(c.moveToNext()){
Log.w("...Provider...",c.getString(0)+"-"+c.getString(1)+"-"+c.getString(2));
}
}
关于android - Android-Content Provider查询游标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26715581/