我正在构建一个小型应用程序,我希望在新数据到达时自动更新。
从我读过的android文档中,我认为是因为我使用了更新的技术,我会在后台创建一个ContentObserver,而我只是自动调用装载程序。
如果有更多有用的信息,我很乐意添加它,或者将整个项目上传到某个地方。
此外,我定位KitKat(sdk版本19)。
此外,在操作栏中,我添加了两个操作,一个用于添加项目,另一个用于清除所有项目。
在添加项目时,ListView不会使用新条目进行更新,我还可以看到ContentProvider上没有运行任何新查询。如果我关闭应用程序并重新启动它,那么这些条目现在可以在ListView中看到。
Activity.java
package us.lynch.listdemo;
import java.text.DateFormat;
import java.util.Date;
import us.lynch.listdemo.R;
import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentValues;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
public class Activity extends ListActivity implements LoaderCallbacks<Cursor> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new SimpleCursorAdapter(this, R.layout.listitem, null, new String[] { "string" }, new int[] { R.id.text }, 0));
getLoaderManager().initLoader(0, null, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
ContentValues values = new ContentValues();
switch (item.getItemId()) {
case R.id.action_add:
values.put("string", DateFormat.getDateTimeInstance().format(new Date()));
getContentResolver().insert(Uri.parse("content://us.lynch.listdemo/data"), values);
return true;
case R.id.action_clear:
getContentResolver().delete(Uri.parse("content://us.lynch.listdemo/data"), null, null);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = Uri.parse("content://us.lynch.listdemo/data");
String[] projection = { "_id", "string" };
return new CursorLoader(this, uri, projection, null, null, "_id");
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
((CursorAdapter) getListAdapter()).swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
((CursorAdapter) getListAdapter()).swapCursor(null);
}
}
ContentProvider.java
package us.lynch.listdemo;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;
public class ContentProvider extends android.content.ContentProvider {
private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private Database mDatabase;
private static final int MATCH = 1;
private static final int MATCH_ID = 2;
static {
sUriMatcher.addURI("us.lynch.listdemo", "data", MATCH);
sUriMatcher.addURI("us.lynch.listdemo", "data/#", MATCH_ID);
}
@Override
public boolean onCreate() {
mDatabase = new Database(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (sUriMatcher.match(uri)) {
case MATCH:
return database().query("data", projection, selection, selectionArgs, null, null, sortOrder);
case MATCH_ID:
selection = addIdToSelection(uri, selection);
return database().query("data", projection, selection, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
switch (sUriMatcher.match(uri)) {
case MATCH: {
return ContentUris.withAppendedId(uri, database().insertOrThrow("data", null, values));
}
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
switch (sUriMatcher.match(uri)) {
case MATCH:
return database().delete("data", selection, selectionArgs);
case MATCH_ID:
selection = addIdToSelection(uri, selection);
return database().delete("data", selection, selectionArgs);
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
switch (sUriMatcher.match(uri)) {
case MATCH:
return database().update("data", values, selection, selectionArgs);
case MATCH_ID:
selection = addIdToSelection(uri, selection);
return database().update("data", values, selection, selectionArgs);
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
@Override
public String getType(Uri uri) {
throw new IllegalArgumentException("Unknown URI " + uri);
}
private SQLiteDatabase database() {
return mDatabase.getWritableDatabase();
}
private static String addIdToSelection(Uri uri, String selection) {
return DatabaseUtils.concatenateWhere(selection, "_id = " + Long.toString(ContentUris.parseId(uri)));
}
}
class Database extends SQLiteOpenHelper {
Database(Context context) {
super(context, "database", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
final String[] columns = new String[] {
"_id integer primary key autoincrement",
"string text not null",
};
database.execSQL("create table data (" + TextUtils.join(",", columns) + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
assert (false);
}
}
RES /布局/ listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
RES /菜单/ main.xml中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_clear"
android:title="Clear Items"
android:visible="true">
</item>
<item
android:id="@+id/action_add"
android:title="Add Item"
android:visible="true">
</item>
</menu>
答案 0 :(得分:5)
您没有使用Uri
通知机制。
在query()方法中写:
Cursor cursor = database().query("data", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
在修改内容的方法(插入,更新,删除)中,通知相同的Uri:
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);