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

时间:2014-10-28 13:03:02

标签: java android android-listfragment simplecursoradapter android-loadermanager

我有一个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类......

`

public class ShoppingListNamesListFragment extends 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很新,所以这可能是我犯过的一个简单错误。但是,基本上,在我看来,在我的notifyChange()类中调用DbContentProvider时,我的ListFragment没有得到通知(或者此区域中还有其他错误)。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

在花了几个小时和几个小时之后,我创建了一个连接到本地联系人内容提供商的TestListActivity - 并且所有工作/更新都应该如此,所以我知道问题可能在我自己的内容提供商中我和# 39;写的。

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

无论如何,所有排序现在! :)