ListView:onContentChanged完成

时间:2013-11-30 10:09:26

标签: android android-listview android-loadermanager

我使用实现LoaderManager的自定义ListFragment构建ListView,自定义ArrayAdapter和自定义AsyncTaskLoader。 我想要做的是从数据库表中加载ListView(可行)。如果我有一个SharedPreference,我在数据库中添加一行,然后通过广播接收器(可以正常工作)请求更新ListView,并最终编辑ListView以隐藏TextView并显示EditText(它没有#t;作品)。 当我试图访问方法中的特定行时

public void onLoadFinished(Loader<List<Favoris>> loader, List<Favoris> data)

我有一个nullPointer异常。我想问题是onContentChanged()方法没有完成,ListView也没有完全更新。 你知道我应该在哪里移动代码的最后一部分吗? (而不是把它放在onLoadFinished中)

编辑:

public static class FavListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<Favoris>> {
  private static final String TAG = "ADP_AppListFragment";
  private static final boolean DEBUG = true;

  // We use a custom ArrayAdapter to bind application info to the ListView.
  private FavoritesListAdapter mAdapter;

  // The Loader's id (this id is specific to the ListFragment's LoaderManager)
  private static final int LOADER_ID = 1;

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

    mAdapter = new FavoritesListAdapter(getActivity());
    setEmptyText("No applications");
    setListAdapter(mAdapter);
    setListShown(false);

    if (DEBUG) {
      Log.i(TAG, "+++ Calling initLoader()! +++");
      if (getLoaderManager().getLoader(LOADER_ID) == null) {
        Log.i(TAG, "+++ Initializing the new Loader... +++");
      } else {
        Log.i(TAG, "+++ Reconnecting with existing Loader (id '1')... +++");
      }
    }

    // Initialize a Loader with id '1'. If the Loader with this id already
    // exists, then the LoaderManager will reuse the existing Loader.
    getLoaderManager().initLoader(LOADER_ID, null, this);
  }

  /**********************/
  /** LOADER CALLBACKS **/
  /**********************/

  @Override
  public Loader<List<Favoris>> onCreateLoader(int id, Bundle args) {
    if (DEBUG) Log.i(TAG, "+++ onCreateLoader() called! +++");
    return new FavorisListLoader(getActivity());
  }

  @Override
  public void onLoadFinished(Loader<List<Favoris>> loader, List<Favoris> data) {
    if (DEBUG) Log.i(TAG, "+++ onLoadFinished() called! +++");
    mAdapter.setData(data);

    if (DEBUG) Log.i(TAG, "+++ setData() called! +++");

    if (isResumed()) {
      setListShown(true);
    } else {
      setListShownNoAnimation(true);
    }


    //addedFav is the favorite item added in the DataBase
    if(addedFav != null) {

        if (DEBUG) Log.i(TAG, "+++ Favoris added : "  + addedFav.getAddress() + " | " + addedFav.getAddressAliasName() );

        int addedFavPos = mAdapter.getPosition(addedFav);

        ListView favListView = this.getListView();

        if (DEBUG) Log.i(TAG, "+++ Listing Item : "  + favListView.toString() );

        if (DEBUG) Log.i(TAG, "+++ ListView Favoris position : "  + addedFavPos );

        if (DEBUG) Log.i(TAG, "+++ ListView Favoris count : " + favListView.getCount() + " +++");

        View vFavoris = this.getListView().getChildAt(addedFavPos);

        TextView favoriteAliasAddressNameTextView = (TextView) vFavoris.findViewById(R.id.addressAliasName);
        favoriteAliasAddressNameTextView.setVisibility(View.GONE);

        EditText favoriteAliasAddressNameEditText = (EditText) vFavoris.findViewById(R.id.addressAliasName_editText);
        favoriteAliasAddressNameEditText.setVisibility(View.VISIBLE);
    }

    if (DEBUG) Log.i(TAG, "+++ editText should be shown ! +++");

    if (DEBUG) Log.i(TAG, "+++ onLoadFinished() FINISHED! +++");
  }

  @Override
  public void onLoaderReset(Loader<List<Favoris>> loader) {
    if (DEBUG) Log.i(TAG, "+++ onLoadReset() called! +++");
    mAdapter.setData(null);
  }

}

0 个答案:

没有答案