gpt4 book ai didi

java - Android ListFragment 数据未通过 SimpleCursorAdapter/LoaderManager/notifyChange() 更新

转载 作者:行者123 更新时间:2023-11-30 11:12:59 31 4
gpt4 key购买 nike

我有一个 ListFragment,它显示存储在我的数据库表中的所有购物 list 的名称。

问题是当我向表中添加新的购物 list 行时,UI 上的 ListFragment 不会自动更新。 (只有关闭并重新启动应用程序才能看到更改。)

首先,这是在我添加购物 list 时在我的 DbContentProvider 类中执行的代码:

`

            // Insert the values into the table
id = db.insert(SHOPPING_LISTS_META_TABLE, null, values);

if (id > -1) {
// Construct and return the URI of the newly inserted row.
Uri insertedId = ContentUris.withAppendedId(CONTENT_URI_SHOPPING_LISTS_META, id);

// Notify any observers of the change in the data set.
Log.d(LOG_TAG, "................. notifyChange(\"" + insertedId + "\", null)");
getContext().getContentResolver().notifyChange(insertedId, null);
Log.d(LOG_TAG, "................. notifyChange() done");
return insertedId;
}
else {
return null;
}

`

...这是它的 LogCat 输出...


10-28 12:29:41.133: D/SQLiteOpenHelper(19401): ..................... notifyChange("content://org.example.myapp.DbContentProvider/shopping_lists_meta/12", 无效)
10-28 12:29:41.143: D/SQLiteOpenHelper(19401): ................notifyChange() 完成
10-28 12:29:41.153: D/HomeActivity(19401): 购物 list ,创建“我的测试购物 list ”:content://org.example.myapp.DbContentProvider/shopping_lists_meta/12
10-28 12:29:41.183: D/AbsListView(19401): unregisterIRListener() 被调用
10-28 12:29:41.193: E/ViewRootImpl(19401): sendUserActionEvent() mView == null
10-28 12:29:41.503: D/AbsListView(19401): unregisterIRListener() 被调用

在 LogCat 中,当我添加新的购物 list 行时,我的 ListFragment 类根本没有输出。这是我的 ListFragment 类...

`

公共(public)类 ShoppingListNamesListFragment 扩展 ListFragment 实现 LoaderManager.LoaderCallbacks {

private final static String LOG_TAG = ShoppingListNamesListFragment.class.getSimpleName(); 

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

// These are the Contacts rows that we will retrieve
static final String[] PROJECTION = {DbContentProvider.KEY_ID,
DbContentProvider.KEY_SHOPPING_LIST_NAME,
DbContentProvider.KEY_IS_SHOPPING_LIST_SELECTED};

// This is the select criteria
static final String SELECTION = null;

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

Log.d(LOG_TAG, "................. onCreate()");

}

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

Log.d(LOG_TAG, "................. onActivityCreated()");

// For the cursor adapter, specify which columns go into which views
String[] fromColumns = {DbContentProvider.KEY_SHOPPING_LIST_NAME};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

// Create an empty adapter we will use to display the loaded data.
// We pass null for the cursor, then update it in onLoadFinished()
mAdapter = new SimpleCursorAdapter(this.getActivity(),
android.R.layout.simple_list_item_1, null,
fromColumns, toViews, 0);
setListAdapter(mAdapter);

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(LOG_TAG, "................. onCreateView()");
return super.onCreateView(inflater, container, savedInstanceState);
}

// Called when a new Loader needs to be created
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d(LOG_TAG, "................. onCreateLoader()");
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
return new CursorLoader(getActivity(), DbContentProvider.CONTENT_URI_SHOPPING_LISTS_META,
PROJECTION, SELECTION, null, null);

}

// Called when a previously created loader has finished loading
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
Log.d(LOG_TAG, "................. onLoaderFinished()");
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}

// Called when a previously created loader is reset, making the data unavailable
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(LOG_TAG, "................. onLoaderReset()");
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
makeToast("shopping list clicked: " + position);
Log.d(LOG_TAG, "shopping list clicked: " + position);
}

private void makeToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
}

注意 - 在我的 DbContentProvider 类中,我有...

public static final Uri CONTENT_URI_SHOPPING_LISTS_META = Uri.parse("content://org.example.myapp.DbContentProvider/shopping_lists_meta");
public static final String SHOPPING_LISTS_META_TABLE = "shopping_lists_meta";

我的代码基于 this Android ListFragment / LoaderManager example .我发现的与此类似的问题都没有提供解决我的问题的解决方案。

我是 Android 的新手,所以这可能是我犯的一个简单错误。但是,基本上,在我看来,当在我的 DbContentProvider 类中调用 notifyChange() 时,我的 ListFragment 没有被通知(或者在这个区域有一些其他错误).谁能帮忙?

最佳答案

在这上面花了几个小时之后,我创建了一个 TestListActivity,它连接到 native 联系人内容提供者 - 并且所有工作/更新都正常,所以我知道问题可能出在我自己的内容提供者中,我' d 写的。

我找到了答案 here .事实证明,我没有在内容提供商的 query() 方法返回的 cursor 上调用 setNotificationUri(ContentResolver cr, Uri uri)。 (我敢肯定这在我工作的 Reto Mauer 书中从未提到过......):/

总之,一切都安排妥当了! :)

关于java - Android ListFragment 数据未通过 SimpleCursorAdapter/LoaderManager/notifyChange() 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26609078/

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