0

我正在尝试使用 Content Provider 让我的 ListView 刷新。我已经创建了我的 Provider 并尝试将它链接到我的 SqlLite DB。一旦我完成了我的提供者,我将实施我的光标加载器,但首先我需要我的提供者的帮助。我还没有为新手用户找到有关如何创建内容提供者的详细信息。任何援助将不胜感激。我的数据库和提供者发布在下​​面。我的主要问题是尝试创建一个链接到我的数据库的 URI。

D B:

 public class dataStore extends SQLiteOpenHelper {

//Table attributes
public static final String DATABASE_NAME = "SiteLogindb";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME_INFOTABLE = "infoTable";

// Data attributes
public static final String COLUMN_NAME_SITE = "sName";
public static final String COLUMN_NAME_ADDRESS = "wUrl";
public static final String COLUMN_NAME_USERNAME = "uName";
public static final String COLUMN_NAME_PASSWORD = "pWord";
public static final String COLUMN_NAME_NOTES = "lNotes";



public dataStore(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        String sqlDataStore = "create table if not exists " +
        TABLE_NAME_INFOTABLE + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_SITE + " text not null,"
                    + COLUMN_NAME_ADDRESS + " text not null,"
                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null,"
                    + COLUMN_NAME_NOTES + " text not null);";

        db.execSQL(sqlDataStore);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        if(oldVersion == 1 && newVersion == 2){
            //Upgrade the database

     }

        }
          }

内容提供商:

           public class ListProvider extends ContentProvider {
private dataStore lDB;

private static final String AUTHORITY = "com.loginplus.home.ListProvider";
public static final int TUTORIALS = 1;
public static final int TUTORIAL_ID = 2;
private static final String TUTORIALS_BASE_PATH = "tutorials";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/" + TUTORIALS_BASE_PATH);
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
        + "/mt-tutorial";
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
        + "/mt-tutorial";

@Override
public boolean onCreate() {
    lDB = new dataStore(getContext());
    return true;
}

private static final UriMatcher sURIMatcher = new UriMatcher(
        UriMatcher.NO_MATCH);
static {
    sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH, TUTORIALS);
    sURIMatcher.addURI(AUTHORITY, TUTORIALS_BASE_PATH + "/#", TUTORIAL_ID);
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(dataStore.TABLE_NAME_INFOTABLE);
    int uriType = sURIMatcher.match(uri);
    switch (uriType) {
    case TUTORIAL_ID:
        queryBuilder.appendWhere(dataStore.DATABASE_VERSION + "="
                + uri.getLastPathSegment());
        break;
    case TUTORIALS:
        // no filter
        break;
    default:
        throw new IllegalArgumentException("Unknown URI");
    }
    Cursor cursor = queryBuilder.query(lDB.getReadableDatabase(),
            projection, selection, selectionArgs, null, null, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
}
//Deleting DB entries
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = lDB.getWritableDatabase();
    int rowsAffected = 0;
    switch (uriType) {
    case TUTORIALS:
        rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                selection, selectionArgs);
        break;
    case TUTORIAL_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                    BaseColums.ID + "=" + id, null);
        } else {
            rowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                    selection + " and " + BaseColums.ID + "=" + id,
                    selectionArgs);
        }
        break;
    default:
        throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsAffected;
}
@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    return null;
}
//Inserting DB entries
@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
            return null;
}
//updating DB entries
@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    int uriType = sURIMatcher.match(uri);
    SQLiteDatabase sqlDB = lDB.getWritableDatabase();
    int rowsAffected = 0;
    switch (uriType) {
    case TUTORIALS:
        rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                null, selection, selectionArgs);
        break;
    case TUTORIAL_ID:
        String id = uri.getLastPathSegment();
        if (TextUtils.isEmpty(selection)) {
            rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                    null, BaseColums.ID + "=" + id, null);
        } else {
            rowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                    null, selection + " and " + BaseColums.ID + "=" + id,
                    selectionArgs);
        }
        break;
    default:
        throw new IllegalArgumentException("Unknown or Invalid URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsAffected;
}
  }
4

1 回答 1

0

我的主要问题是尝试创建一个链接到我的数据库的 URI。

  • 现在,您ListProvider.CONTENT_URI将匹配您ListProvider使用的UriMatcher. 您ListProvider正在使用您dataStore.TABLE_NAME_INFOTABLE的数据库中唯一的表。

  • 因此,您已经可以通过在您的代码中ContentProvider使用 a 来直接使用ContentResolverActivity的代码,如下所示:

     getContentResolver().query(ListProvider.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
     getContentResolver().delete(ListProvider.CONTENT_URI, where, selectionArgs);
     //etc...
    

一旦我完成了我的提供者,我将实施我的光标加载器,但首先我需要我的提供者的帮助。

  • 你的ContentProvider似乎完成了。但是,您必须记住使用相同的AUTHORITY. 您可以重命名权限,但它必须是唯一的,并且在您的 ContentProvider 和您的清单中匹配。

    <provider
        android:name="com.loginplus.home.ListProvider"
        android:authorities="com.loginplus.home.ListProvider" >
    </provider>
    
  • 然后,您将能够CursorLoaderpublic Loader<Cursor> onCreateLoader(int id, Bundle args)回调中创建您的。

    CursorLoader cursorLoader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, selection, selectionArgs, orderBy);
    

我还没有为新手用户找到有关如何创建内容提供者的详细信息。任何援助将不胜感激。

  • Lars Vogella 有一个很棒的教程,可以引导您创建自己的SQLite databaseContentProvider. 和CursorLoader. 这帮助我开始并给了我很好的基础。
于 2013-03-05T19:39:54.167 回答