Sqlite_Content Provider assistant

2019-08-21 16:29发布

I am trying to get my ListView to refresh by using a Content Provider. I have created my Provider and have tried to link it propelry to my SqlLite DB. Once I have my provider completed I will implement my cursorloader but 1st I need help with my provider. I have not found and detailed information for novice users on how to create a content provider. any assistance would be appreciated. My DB and Provider are posted below. My main issue is trying to create a URI that links to my DB.

DB:

 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

     }

        }
          }

Content Provider:

           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;
}
  }

1条回答
Evening l夕情丶
2楼-- · 2019-08-21 17:06

My main issue is trying to create a URI that links to my DB.

  • Right now your ListProvider.CONTENT_URI will match in your ListProvider using your UriMatcher. And your ListProvider is using your dataStore.TABLE_NAME_INFOTABLE which is the only table in your database.

  • So you can already use your ContentProvider directly by using a ContentResolver in your Activity with code like this:

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

Once I have my provider completed I will implement my cursorloader but 1st I need help with my provider.

  • Your ContentProvider appears to be done. However, you must remember to add it to your manifest with the same AUTHORITY. You can rename the authority, but it must be unique and match in your `ContentProvider and your manifest.

    <provider
        android:name="com.loginplus.home.ListProvider"
        android:authorities="com.loginplus.home.ListProvider" >
    </provider>
    
  • You will then be able to create your CursorLoader in the public Loader<Cursor> onCreateLoader(int id, Bundle args) callback.

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

I have not found and detailed information for novice users on how to create a content provider. any assistance would be appreciated.

  • Lars Vogella has a great tutorial that walks you through creating your own SQLite database, ContentProvider, and CursorLoader. This helped me get started and gave me a great basis.
查看更多
登录 后发表回答