有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

首先,这是我添加购物清单时在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", null) 10-28 12:29:41.143: D/SQLiteOpenHelper(19401): ................. notifyChange() done 10-28 12:29:41.153: D/HomeActivity(19401): Shopping list, "My Test Shopping List" created: content://org.example.myapp.DbContentProvider/shopping_lists_meta/12 10-28 12:29:41.183: D/AbsListView(19401): unregisterIRListener() is called 10-28 12:29:41.193: E/ViewRootImpl(19401): sendUserActionEvent() mView == null 10-28 12:29:41.503: D/AbsListView(19401): unregisterIRListener() is called

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

`

公共类ShoppingListNamesListFragment扩展ListFragment实现LoaderManager。装载机回扣{

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 = {安卓.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(), 
            安卓.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。我发现,没有一个类似的问题能解决我的问题

我对安卓相当陌生,所以这可能是我犯的一个简单错误。但是,本质上,在我看来,当notifyChange()在我的DbContentProvider类中被调用时,我的ListFragment没有被通知(或者在这个区域有其他一些错误)。有人能帮忙吗


共 (1) 个答案

  1. # 1 楼答案

    在花了好几个小时做这件事之后,我创建了一个TestListActivity,它连接到了原生联系人内容提供商——所有这些都正常工作/更新,所以我知道问题可能出在我自己编写的内容提供商身上

    我找到了答案。事实证明,我没有对我的内容提供者的query()方法返回的cursor调用setNotificationUri(ContentResolver cr, Uri uri)。(我敢肯定,这在我写的雷托·摩尔的书中从来没有提到过……):

    总之,现在一切都好了!:)