首页 > 编程语言 >Android 装载器---在应用程序中使用装载器

Android 装载器---在应用程序中使用装载器

时间:2022-12-09 21:05:28浏览次数:37  
标签:--- ID 装载 public MainTable new import Android android

这一节描述了在Android应用程序中如何使用装载器,使用装载器的应用程序通常包括以下内容:

1. 一个Activity或Fragment;
2. 一个LoaderManager实例;
3. 一个用于加载通过ContentProvider保存的数据的CursorLoader对象,也可以是你自己实现的用于加载其他类型数据的Loader或AsyncTaskLoader类的子类。
4. 一个LoaderManager.LoaderCallbacks相关的实现,这是你创建新的装载器和管理既存装载器引用的地方;
5. 一个显示装载器数据的方法,如一个SimpleCursorAdapter;
6. 一个数据源,如CursorLoader使用的ContentProvider对象。


通常在Activity的onCreate()方法或Fragment的onActivityCreated()内初始化一个装载器,如:
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
initLoader方法带有以下参数:
1. 标识这个装载器的唯一ID,这个例子中ID是“0”;
2. 可选参数,它是给装载器提供的在构造时所需的参数,这个例子中是null;
3. 一个LoaderManager.LoaderCallbacks的实现,LoaderManager会调用这个实现来报告装载器事件。在这个例子中,这个类实现了LoaderManager.LoaderCallbacks接口,因此它把它自己(this)的引用传给了initLoader()方法。
initLoader()调用确保了装载器被初始化和激活。它有两种可能的结果:
1. 如果通过ID指定的装载器已经存在,那么最后被创建的装载器被重用。
2. 如果通过ID指定的装载器不存在,initLoader()方法会触发LoaderManager.LoaderCallbacks的onCreateLoader()方法。这是你实现初始化代码的地方,并且返回一个新的装载器。关于更多的讨论,请看onCreateLoader()相关章节。
在这两种情况下,都给LoaderManager.LoaderCallbacks实现了与装载器的关联,并且在装载器状态改变的时候,相关的回调方法会被调用。如果在调用的时点调用者是在被启动的状态,并且被请求的装载器已经存在且已经生成了相关的数据,那么系统会立即调用onLoadFinished()(在initLoader()方执行期间),因此你必须为这种情况做好准备。对于这个回调的更多讨论,请看onloadFinished()回调。

注意:initLoader()方法返回一个被创建的Loader对象,但是你不需要获取关于这个对象的引用,因为LoaderManager对象自动的管理着装载器的生存。LoaderManager对象在需要的时候启动和终止装载器,并且保持着装载器的状态和相关匹配的内容。这就意味着,你很少直接跟装载器交互(虽然可以使用装载器方法微调装载器的行为,请参阅例子LoaderThrottle),大多数情况都是使用LoaderManagerLoaderCallbacks的回调方法在加载过程中发生特定事件时来进行干预。关于更多的讨论,可参阅使用LoaderManager回调方法(Using the LoaderManager Callbacks)
例子:LoaderThrottle.java
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.apis.app;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.util.HashMap;

/**
* Demonstration of bottom to top implementation of a content provider holding
* structured data through displaying it in the UI, using throttling to reduce
* the number of queries done when its data changes.
*/
public class LoaderThrottle extends Activity {
// Debugging.
static final String TAG = "LoaderThrottle";

/**
* The authority we use to get to our sample provider.
*/
public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle";

/**
* Definition of the contract for the main table of our provider.
*/
public static final class MainTable implements BaseColumns {

// This class cannot be instantiated
private MainTable() {}

/**
* The table name offered by this provider
*/
public static final String TABLE_NAME = "main";

/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/main");

/**
* The content URI base for a single row of data. Callers must
* append a numeric row id to this Uri to retrieve a row
*/
public static final Uri CONTENT_ID_URI_BASE
= Uri.parse("content://" + AUTHORITY + "/main/");

/**
* The MIME type of {@link #CONTENT_URI}.
*/
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.example.api-demos-throttle";

/**
* The MIME type of a {@link #CONTENT_URI} sub-directory of a single row.
*/
public static final String CONTENT_ITEM_TYPE
= "vnd.android.cursor.item/vnd.example.api-demos-throttle";
/**
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";

/**
* Column name for the single column holding our data.
* <P>Type: TEXT</P>
*/
public static final String COLUMN_NAME_DATA = "data";
}

/**
* This class helps open, create, and upgrade the database file.
*/
static class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "loader_throttle.db";
private static final int DATABASE_VERSION = 2;

DatabaseHelper(Context context) {

// calls the super constructor, requesting the default cursor factory.
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**
*
* Creates the underlying database with table name and column names taken from the
* NotePad class.
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + MainTable.TABLE_NAME + " ("
+ MainTable._ID + " INTEGER PRIMARY KEY,"
+ MainTable.COLUMN_NAME_DATA + " TEXT"
+ ");");
}

/**
*
* Demonstrates that the provider must consider what happens when the
* underlying datastore is changed. In this sample, the database is upgraded the database
* by destroying the existing data.
* A real application should upgrade the database in place.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// Logs that the database is being upgraded
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");

// Kills the table and existing data
db.execSQL("DROP TABLE IF EXISTS notes");

// Recreates the database with a new version
onCreate(db);
}
}

/**
* A very simple implementation of a content provider.
*/
public static class SimpleProvider extends ContentProvider {
// A projection map used to select columns from the database
private final HashMap<String, String> mNotesProjectionMap;
// Uri matcher to decode incoming URIs.
private final UriMatcher mUriMatcher;

// The incoming URI matches the main table URI pattern
private static final int MAIN = 1;
// The incoming URI matches the main table row ID URI pattern
private static final int MAIN_ID = 2;

// Handle to a new DatabaseHelper.
private DatabaseHelper mOpenHelper;

/**
* Global provider initialization.
*/
public SimpleProvider() {
// Create and initialize URI matcher.
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID);

// Create and initialize projection map for all columns. This is
// simply an identity mapping.
mNotesProjectionMap = new HashMap<String, String>();
mNotesProjectionMap.put(MainTable._ID, MainTable._ID);
mNotesProjectionMap.put(MainTable.COLUMN_NAME_DATA, MainTable.COLUMN_NAME_DATA);
}

/**
* Perform provider creation.
*/
@Override
public boolean onCreate() {
mOpenHelper = new DatabaseHelper(getContext());
// Assumes that any failures will be reported by a thrown exception.
return true;
}

/**
* Handle incoming queries.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {

// Constructs a new query builder and sets its table name
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(MainTable.TABLE_NAME);

switch (mUriMatcher.match(uri)) {
case MAIN:
// If the incoming URI is for main table.
qb.setProjectionMap(mNotesProjectionMap);
break;

case MAIN_ID:
// The incoming URI is for a single row.
qb.setProjectionMap(mNotesProjectionMap);
qb.appendWhere(MainTable._ID + "=?");
selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
new String[] { uri.getLastPathSegment() });
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}


if (TextUtils.isEmpty(sortOrder)) {
sortOrder = MainTable.DEFAULT_SORT_ORDER;
}

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

Cursor c = qb.query(db, projection, selection, selectionArgs,
null /* no group */, null /* no filter */, sortOrder);

c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}

/**
* Return the MIME type for an known URI in the provider.
*/
@Override
public String getType(Uri uri) {
switch (mUriMatcher.match(uri)) {
case MAIN:
return MainTable.CONTENT_TYPE;
case MAIN_ID:
return MainTable.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}

/**
* Handler inserting new data.
*/
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
if (mUriMatcher.match(uri) != MAIN) {
// Can only insert into to main URI.
throw new IllegalArgumentException("Unknown URI " + uri);
}

ContentValues values;

if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}

if (values.containsKey(MainTable.COLUMN_NAME_DATA) == false) {
values.put(MainTable.COLUMN_NAME_DATA, "");
}

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

long rowId = db.insert(MainTable.TABLE_NAME, null, values);

// If the insert succeeded, the row ID exists.
if (rowId > 0) {
Uri noteUri = ContentUris.withAppendedId(MainTable.CONTENT_ID_URI_BASE, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
return noteUri;
}

throw new SQLException("Failed to insert row into " + uri);
}

/**
* Handle deleting data.
*/
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String finalWhere;

int count;

switch (mUriMatcher.match(uri)) {
case MAIN:
// If URI is main table, delete uses incoming where clause and args.
count = db.delete(MainTable.TABLE_NAME, where, whereArgs);
break;

// If the incoming URI matches a single note ID, does the delete based on the
// incoming data, but modifies the where clause to restrict it to the
// particular note ID.
case MAIN_ID:
// If URI is for a particular row ID, delete is based on incoming
// data but modified to restrict to the given ID.
finalWhere = DatabaseUtils.concatenateWhere(
MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);

return count;
}

/**
* Handle updating data.
*/
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
String finalWhere;

switch (mUriMatcher.match(uri)) {
case MAIN:
// If URI is main table, update uses incoming where clause and args.
count = db.update(MainTable.TABLE_NAME, values, where, whereArgs);
break;

case MAIN_ID:
// If URI is for a particular row ID, update is based on incoming
// data but modified to restrict to the given ID.
finalWhere = DatabaseUtils.concatenateWhere(
MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);

return count;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FragmentManager fm = getFragmentManager();

// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}

public static class ThrottledLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> {

// Menu identifiers
static final int POPULATE_ID = Menu.FIRST;
static final int CLEAR_ID = Menu.FIRST+1;

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;

// If non-null, this is the current filter the user has provided.
String mCurFilter;

// Task we have running to populate the database.
AsyncTask<Void, Void, Void> mPopulatingTask;

@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

setEmptyText("No data. Select 'Populate' to fill with data from Z to A at a rate of 4 per second.");
setHasOptionsMenu(true);

// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { MainTable.COLUMN_NAME_DATA },
new int[] { android.R.id.text1 }, 0);
setListAdapter(mAdapter);

// Start out with a progress indicator.
setListShown(false);

// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(Menu.NONE, POPULATE_ID, 0, "Populate")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, CLEAR_ID, 0, "Clear")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

@Override public boolean onOptionsItemSelected(MenuItem item) {
final ContentResolver cr = getActivity().getContentResolver();

switch (item.getItemId()) {
case POPULATE_ID:
if (mPopulatingTask != null) {
mPopulatingTask.cancel(false);
}
mPopulatingTask = new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
for (char c='Z'; c>='A'; c--) {
if (isCancelled()) {
break;
}
StringBuilder builder = new StringBuilder("Data ");
builder.append(c);
ContentValues values = new ContentValues();
values.put(MainTable.COLUMN_NAME_DATA, builder.toString());
cr.insert(MainTable.CONTENT_URI, values);
// Wait a bit between each insert.
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
return null;
}
};
mPopulatingTask.executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
return true;

case CLEAR_ID:
if (mPopulatingTask != null) {
mPopulatingTask.cancel(false);
mPopulatingTask = null;
}
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
cr.delete(MainTable.CONTENT_URI, null, null);
return null;
}
};
task.execute((Void[])null);
return true;

default:
return super.onOptionsItemSelected(item);
}
}

@Override public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i(TAG, "Item clicked: " + id);
}

// These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] {
MainTable._ID,
MainTable.COLUMN_NAME_DATA,
};

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,
PROJECTION, null, null, null);
cl.setUpdateThrottle(2000); // update at most every 2 seconds.
return cl;
}

public void onl oadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);

// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}

public void onl oaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
}
分享到:











LoaderManager管理Activity或Fragment内部的一个或多个装载器实例,每个Activity或Fragment只有一个LoaderManager对象。

通常在Activity的onCreate()方法或Fragment的onActivityCreated()内初始化一个装载器,如:
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
initLoader方法带有以下参数:
1. 标识这个装载器的唯一ID,这个例子中ID是“0”;
2. 可选参数,它是给装载器提供的在构造时所需的参数,这个例子中是null;
3. 一个LoaderManager.LoaderCallbacks的实现,LoaderManager会调用这个实现来报告装载器事件。在这个例子中,这个类实现了LoaderManager.LoaderCallbacks接口,因此它把它自己(this)的引用传给了initLoader()方法。
initLoader()调用确保了装载器被初始化和激活。它有两种可能的结果:
1. 如果通过ID指定的装载器已经存在,那么最后被创建的装载器被重用。
2. 如果通过ID指定的装载器不存在,initLoader()方法会触发LoaderManager.LoaderCallbacks的onCreateLoader()方法。这是你实现初始化代码的地方,并且返回一个新的装载器。关于更多的讨论,请看onCreateLoader()相关章节。
在这两种情况下,都给LoaderManager.LoaderCallbacks实现了与装载器的关联,并且在装载器状态改变的时候,相关的回调方法会被调用。如果在调用的时点调用者是在被启动的状态,并且被请求的装载器已经存在且已经生成了相关的数据,那么系统会立即调用onLoadFinished()(在initLoader()方执行期间),因此你必须为这种情况做好准备。对于这个回调的更多讨论,请看onloadFinished()回调。

注意:initLoader()方法返回一个被创建的Loader对象,但是你不需要获取关于这个对象的引用,因为LoaderManager对象自动的管理着装载器的生存。LoaderManager对象在需要的时候启动和终止装载器,并且保持着装载器的状态和相关匹配的内容。这就意味着,你很少直接跟装载器交互(虽然可以使用装载器方法微调装载器的行为,请参阅例子LoaderThrottle),大多数情况都是使用LoaderManagerLoaderCallbacks的回调方法在加载过程中发生特定事件时来进行干预。关于更多的讨论,可参阅使用LoaderManager回调方法(Using the LoaderManager Callbacks)
例子:LoaderThrottle.java
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.apis.app;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import java.util.HashMap;

/**
* Demonstration of bottom to top implementation of a content provider holding
* structured data through displaying it in the UI, using throttling to reduce
* the number of queries done when its data changes.
*/
public class LoaderThrottle extends Activity {
// Debugging.
static final String TAG = "LoaderThrottle";

/**
* The authority we use to get to our sample provider.
*/
public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle";

/**
* Definition of the contract for the main table of our provider.
*/
public static final class MainTable implements BaseColumns {

// This class cannot be instantiated
private MainTable() {}

/**
* The table name offered by this provider
*/
public static final String TABLE_NAME = "main";

/**
* The content:// style URL for this table
*/
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/main");

/**
* The content URI base for a single row of data. Callers must
* append a numeric row id to this Uri to retrieve a row
*/
public static final Uri CONTENT_ID_URI_BASE
= Uri.parse("content://" + AUTHORITY + "/main/");

/**
* The MIME type of {@link #CONTENT_URI}.
*/
public static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.example.api-demos-throttle";

/**
* The MIME type of a {@link #CONTENT_URI} sub-directory of a single row.
*/
public static final String CONTENT_ITEM_TYPE
= "vnd.android.cursor.item/vnd.example.api-demos-throttle";
/**
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";

/**
* Column name for the single column holding our data.
* <P>Type: TEXT</P>
*/
public static final String COLUMN_NAME_DATA = "data";
}

/**
* This class helps open, create, and upgrade the database file.
*/
static class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "loader_throttle.db";
private static final int DATABASE_VERSION = 2;

DatabaseHelper(Context context) {

// calls the super constructor, requesting the default cursor factory.
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

/**
*
* Creates the underlying database with table name and column names taken from the
* NotePad class.
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + MainTable.TABLE_NAME + " ("
+ MainTable._ID + " INTEGER PRIMARY KEY,"
+ MainTable.COLUMN_NAME_DATA + " TEXT"
+ ");");
}

/**
*
* Demonstrates that the provider must consider what happens when the
* underlying datastore is changed. In this sample, the database is upgraded the database
* by destroying the existing data.
* A real application should upgrade the database in place.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// Logs that the database is being upgraded
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");

// Kills the table and existing data
db.execSQL("DROP TABLE IF EXISTS notes");

// Recreates the database with a new version
onCreate(db);
}
}

/**
* A very simple implementation of a content provider.
*/
public static class SimpleProvider extends ContentProvider {
// A projection map used to select columns from the database
private final HashMap<String, String> mNotesProjectionMap;
// Uri matcher to decode incoming URIs.
private final UriMatcher mUriMatcher;

// The incoming URI matches the main table URI pattern
private static final int MAIN = 1;
// The incoming URI matches the main table row ID URI pattern
private static final int MAIN_ID = 2;

// Handle to a new DatabaseHelper.
private DatabaseHelper mOpenHelper;

/**
* Global provider initialization.
*/
public SimpleProvider() {
// Create and initialize URI matcher.
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID);

// Create and initialize projection map for all columns. This is
// simply an identity mapping.
mNotesProjectionMap = new HashMap<String, String>();
mNotesProjectionMap.put(MainTable._ID, MainTable._ID);
mNotesProjectionMap.put(MainTable.COLUMN_NAME_DATA, MainTable.COLUMN_NAME_DATA);
}

/**
* Perform provider creation.
*/
@Override
public boolean onCreate() {
mOpenHelper = new DatabaseHelper(getContext());
// Assumes that any failures will be reported by a thrown exception.
return true;
}

/**
* Handle incoming queries.
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {

// Constructs a new query builder and sets its table name
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(MainTable.TABLE_NAME);

switch (mUriMatcher.match(uri)) {
case MAIN:
// If the incoming URI is for main table.
qb.setProjectionMap(mNotesProjectionMap);
break;

case MAIN_ID:
// The incoming URI is for a single row.
qb.setProjectionMap(mNotesProjectionMap);
qb.appendWhere(MainTable._ID + "=?");
selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
new String[] { uri.getLastPathSegment() });
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}


if (TextUtils.isEmpty(sortOrder)) {
sortOrder = MainTable.DEFAULT_SORT_ORDER;
}

SQLiteDatabase db = mOpenHelper.getReadableDatabase();

Cursor c = qb.query(db, projection, selection, selectionArgs,
null /* no group */, null /* no filter */, sortOrder);

c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}

/**
* Return the MIME type for an known URI in the provider.
*/
@Override
public String getType(Uri uri) {
switch (mUriMatcher.match(uri)) {
case MAIN:
return MainTable.CONTENT_TYPE;
case MAIN_ID:
return MainTable.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}

/**
* Handler inserting new data.
*/
@Override
public Uri insert(Uri uri, ContentValues initialValues) {
if (mUriMatcher.match(uri) != MAIN) {
// Can only insert into to main URI.
throw new IllegalArgumentException("Unknown URI " + uri);
}

ContentValues values;

if (initialValues != null) {
values = new ContentValues(initialValues);
} else {
values = new ContentValues();
}

if (values.containsKey(MainTable.COLUMN_NAME_DATA) == false) {
values.put(MainTable.COLUMN_NAME_DATA, "");
}

SQLiteDatabase db = mOpenHelper.getWritableDatabase();

long rowId = db.insert(MainTable.TABLE_NAME, null, values);

// If the insert succeeded, the row ID exists.
if (rowId > 0) {
Uri noteUri = ContentUris.withAppendedId(MainTable.CONTENT_ID_URI_BASE, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
return noteUri;
}

throw new SQLException("Failed to insert row into " + uri);
}

/**
* Handle deleting data.
*/
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String finalWhere;

int count;

switch (mUriMatcher.match(uri)) {
case MAIN:
// If URI is main table, delete uses incoming where clause and args.
count = db.delete(MainTable.TABLE_NAME, where, whereArgs);
break;

// If the incoming URI matches a single note ID, does the delete based on the
// incoming data, but modifies the where clause to restrict it to the
// particular note ID.
case MAIN_ID:
// If URI is for a particular row ID, delete is based on incoming
// data but modified to restrict to the given ID.
finalWhere = DatabaseUtils.concatenateWhere(
MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);

return count;
}

/**
* Handle updating data.
*/
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int count;
String finalWhere;

switch (mUriMatcher.match(uri)) {
case MAIN:
// If URI is main table, update uses incoming where clause and args.
count = db.update(MainTable.TABLE_NAME, values, where, whereArgs);
break;

case MAIN_ID:
// If URI is for a particular row ID, update is based on incoming
// data but modified to restrict to the given ID.
finalWhere = DatabaseUtils.concatenateWhere(
MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);
break;

default:
throw new IllegalArgumentException("Unknown URI " + uri);
}

getContext().getContentResolver().notifyChange(uri, null);

return count;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FragmentManager fm = getFragmentManager();

// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}

public static class ThrottledLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> {

// Menu identifiers
static final int POPULATE_ID = Menu.FIRST;
static final int CLEAR_ID = Menu.FIRST+1;

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;

// If non-null, this is the current filter the user has provided.
String mCurFilter;

// Task we have running to populate the database.
AsyncTask<Void, Void, Void> mPopulatingTask;

@Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

setEmptyText("No data. Select 'Populate' to fill with data from Z to A at a rate of 4 per second.");
setHasOptionsMenu(true);

// Create an empty adapter we will use to display the loaded data.
mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] { MainTable.COLUMN_NAME_DATA },
new int[] { android.R.id.text1 }, 0);
setListAdapter(mAdapter);

// Start out with a progress indicator.
setListShown(false);

// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}

@Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(Menu.NONE, POPULATE_ID, 0, "Populate")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add(Menu.NONE, CLEAR_ID, 0, "Clear")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}

@Override public boolean onOptionsItemSelected(MenuItem item) {
final ContentResolver cr = getActivity().getContentResolver();

switch (item.getItemId()) {
case POPULATE_ID:
if (mPopulatingTask != null) {
mPopulatingTask.cancel(false);
}
mPopulatingTask = new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
for (char c='Z'; c>='A'; c--) {
if (isCancelled()) {
break;
}
StringBuilder builder = new StringBuilder("Data ");
builder.append(c);
ContentValues values = new ContentValues();
values.put(MainTable.COLUMN_NAME_DATA, builder.toString());
cr.insert(MainTable.CONTENT_URI, values);
// Wait a bit between each insert.
try {
Thread.sleep(250);
} catch (InterruptedException e) {
}
}
return null;
}
};
mPopulatingTask.executeOnExecutor(
AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
return true;

case CLEAR_ID:
if (mPopulatingTask != null) {
mPopulatingTask.cancel(false);
mPopulatingTask = null;
}
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override protected Void doInBackground(Void... params) {
cr.delete(MainTable.CONTENT_URI, null, null);
return null;
}
};
task.execute((Void[])null);
return true;

default:
return super.onOptionsItemSelected(item);
}
}

@Override public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i(TAG, "Item clicked: " + id);
}

// These are the rows that we will retrieve.
static final String[] PROJECTION = new String[] {
MainTable._ID,
MainTable.COLUMN_NAME_DATA,
};

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,
PROJECTION, null, null, null);
cl.setUpdateThrottle(2000); // update at most every 2 seconds.
return cl;
}

public void onl oadFinished(Loader<Cursor> loader, Cursor data) {
mAdapter.swapCursor(data);

// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}

public void onl oaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
}
}

标签:---,ID,装载,public,MainTable,new,import,Android,android
From: https://blog.51cto.com/u_15907753/5926512

相关文章

  • Android GestureDetector手势识别类
    为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过​​GestureDetector.OnGestureListener​​来获取当前被触发的操作手势(SingleTapUp、ShowPress、Lo......
  • Android中的横竖屏、资源、国际化的使用
    Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layout与values目录,layout里的xml文件的我们应用使用布局的文件,values里......
  • 设计模式-结构型模式
    结构型模式:将对象和类组装成较⼤的结构,并同时保持结构的灵活和⾼效。PS:博客根据it老齐大话设计模式课程课件进行整理,IT老齐视频学习网站:https://www.itlaoqi.com包含......
  • bugku刷题--Web
    0x00 滑稽     入门CTF的第一道题目(也是在CSDN上直接被剧透WriteUp)。     启动场景后是滑稽/滑稽             在谷歌浏......
  • 利用JNI加载paddle-ocr进行ocr识别
    利用JNI加载paddle-ocr进行ocr识别项目地址:https://github.com/jiangnanboy/java-springboot-paddleocr-v2TableofContentsAboutGettingStartedResultContactRef......
  • BLOG-3总结
    一、前言通过最近的学习,java大致的内容都有一定的了解与练习。这几次的作业难度较大,不过按照老师的类图,经行分类,写起来的难度降低了不少。题目集六:①、座机计费题目,通过......
  • javascript-代码随想录训练营day16
    104.二叉树的最大深度题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/题目描述:给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子......
  • HCIA-Cloud-01
    HCIA-Cloud01-上午云计算简介华为的官网,这些是公有云能够提供的服务。基于华为自主研发的芯片的。华为自主研发的AI智能芯片。如图这里有面向各种业务类型提供......
  • 分布式定时调度:xxl-job
    背景有服务里面在跑定时任务,一直是单点在运行,虽然存在挺大的风险,但也这样扛下来了。但是呢,现在要做多点了,springboot的Scheduled,虽然好用,在多点就会存在一些问题,多个......
  • 函数调用--传值调用和传址调用
    intget_max(intx,inty)//返回整型用int,x和y是形式参数。{ return(x>y)?(x):(y);}//voidswap1(int*x,int*y)voidswap2(int&x,int&y){inttmp=0;tmp=......