Skip to content Skip to sidebar Skip to footer

Unable To Refresh Listview Data Within Same Fragments Used Under Different Tabs

I have implemented tabs functionality via SmartTabLayout library in my android application. At present I have used same fragment as viewpager for both of my tabs. Since, the only d

Solution 1:

I think your problem is here:

mAdapter = new ItemsListAdapter(getActivity(),lstItems);

You create a new instance of ItemsListAdapter and bind it to the listview with

lstviewItems.setAdapter(mAdapter);

The problem is that this adapter is static. So if you create a new instance you destroy the old adapter and the listview of the other tab has not the adapter anymore that updates your data.

EDIT:

I'd recommend to load the data on a central place. Add the response (your data objects) to a manager class. Then implement on every view which using this data a callback (lets say JsonDataChangedCallback). Register the classes which implementing the callback to the manager with Manager.getInstance().registerCallback(). Then every time your data is changed call updateCallbacks() in your manager and all views will be updated. That's the way implemented that process in my app and it works.

Sample Code:

publicclassCDMMovieManager {
  privateCDMMovieManager() {
    m_Movies = new ArrayList<>();
    m_MovieChangedCallbacks = new ArrayList<>();
  }

  publicstatic synchronized CDMMovieManager getInstance() {
    if(ms_Instance == null)
      ms_Instance = new CDMMovieManager();

    return ms_Instance;
  }

  publicvoidaddMovie(CDMMovie p_Movie) {
    m_Movies.add(p_Movie);
    notifyCallbacks();
  }

  /**
   * Registers a movie changed callback
   *
   * @param p_MovieChangedCallback the callback to register
   */publicvoidregisterMovieChangedCallback(IDMMovieChangedCallback p_MovieChangedCallback) {
    m_MovieChangedCallbacks.add(p_MovieChangedCallback);
  }

  /**
   * Removes a movie changed callback
   *
   * @param p_MovieChangedCallback the callback to remove
   */publicvoidremoveMovieChangedCallback(IDMMovieChangedCallback p_MovieChangedCallback) {
    m_MovieChangedCallbacks.remove(p_MovieChangedCallback);
  }

  privatevoidnotifyCallbacks() {
    for(IDMMovieChangedCallback l_MovieChangedCallback : m_MovieChangedCallbacks) {
      l_MovieChangedCallback.onMoviesChanged();
    }
  }
}

And the implementing Class:

publicclassCDMBasicMovieFragmentextendsFragmentimplementsIDMMovieChangedCallback {

//...@OverridepublicvoidonMoviesChanged() {
    m_Adapter.notifyDataSetChanged();
  }
}

Post a Comment for "Unable To Refresh Listview Data Within Same Fragments Used Under Different Tabs"