gpt4 book ai didi

android - ListActivity 不会随着数据到达而更新

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:48:11 27 4
gpt4 key购买 nike

我正在构建一个小型应用程序,我想在新数据到达时自动更新它。

根据我阅读的 android 文档,我认为因为我使用的是较新的技术,所以会在后台为我创建一个 ContentObserver,并且我会自动调用 Loader。

如果有更多有用的信息,我很乐意添加,或者将完整的项目上传到某处。

此外,我的目标是 KitKat(SDK 版本 19)。

应用概览

  1. 我们有一个使用内置 ListView 的 ListActivity。
  2. ListActivity 使用 Loader 和 SimpleCursorAdapter 获取数据。
  3. Loader 通过查询 ContentProvider 检索其数据。
  4. ContentProvider 由 SQLite 数据库支持。

此外,在操作栏中,我添加了两个操作,一个用于添加项目,一个用于清除所有项目。

当我添加项目时,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/layout/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/menu/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>

最佳答案

您不使用 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);

关于android - ListActivity 不会随着数据到达而更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24087741/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com