我似乎无法掌握内容提供商的概念。我个人认为它使代码膨胀,它使它过于复杂,而且显然非常糟糕。
BTW这将是您在网上找到的多表ContentProvider的第一个示例代码。绝对没有其他,除了可能是ContactsProvider(" veeeerrrrry有用" - 通过超级填充和不可识别)。我相信像我这样的许多新手都会欣赏这一点。
请帮我从内容提供商处制作一个简单的光标。我有正常的代码(使用简单的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);
}
}
});
所以我需要从内容提供者那里获得光标。我已经开始开发这个&#34;令人兴奋的&#34;内容提供者,但我不知道我在做什么是好的,因为它太复杂了。在这里它(我的内容提供者需要引用多个表):
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);
}
}
我试图&#34;翻译&#34;记录光标内容的活动代码,但它只是没有。这非常令人沮丧。 这是我的翻译&#34;:
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));
}
但我不想在内容提供商中这样做,我想在活动代码中这样做。 我做错了什么?
请帮忙。
谢谢
答案 0 :(得分:2)
如果您不必,则不必使用ContentProvider
。确定您的要求。如果数据对您的应用程序是私有的,并且必须以表格格式存储,那么您可以直接在Android中访问SQLite引擎,而无需通过ContentProvider
。但是,如果您希望其他应用或系统访问您应用的数据,那么是 - 这是ContentProvider
的合法用例。
尽管如此,我建议您阅读此article - 告诉您如何使用SQLite
访问ContentProvider
引擎{/ 1}}。
希望这有帮助。
无论如何,对于你手头的问题,试试这个 -
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));
}
}
答案 1 :(得分:-1)
而不是这个
btnp.setOnClickListener(new View.OnClickListener
尝试
btnp.setOnClickListener(this)
并将onClickListner实现到Activity。
简而言之,不是将onClickListener强制转换为按钮,而是将其实现到整个活动并尝试。