2

I have a PagerAdapter that takes in a Cursor in the constructor and uses that cursor to get the values from the database to populate the views.

This is what I am doing:

  • On 5 times swipe down action of the view page, I set the visibility of one of the view to be invisible.
  • To save the count I just used the preferences where I increment and the count.
  • In the onResumeFragment(), I do a check if the count is some_number and I do a adapter.notifyDataSetChanged(). I do have extra check on the adapter level as well.
  • On calling the notifyDataSetChanged(), the index moves to the last item in an array and I am assuming its because of the notifyChanged() in DataSetObservable class.
  • notifyChanged() is actually called when we do call the notifyDataSetChanged(). Notify Changed method matches the list in the reverse order and that' probably the reason I am getting the last index when calling the notifyDataSetChanged().

This is how the code for notifyChanged() looks like from the source:

/**
     * Invokes {@link DataSetObserver#onChanged} on each observer.
     * Called when the contents of the data set have changed.  The recipient
     * will obtain the new contents the next time it queries the data set.
     */
    public void notifyChanged() {
        synchronized(mObservers) {
            // since onChanged() is implemented by the app, it could do anything, including
            // removing itself from {@link mObservers} - and that could cause problems if
            // an iterator is used on the ArrayList {@link mObservers}.
            // to avoid such problems, just march thru the list in the reverse order.
            for (int i = mObservers.size() - 1; i >= 0; i--) {
                mObservers.get(i).onChanged();
            }
        }
    }

What I want to achieve :

  • After calling the notifyDataSetChanged(), my view pager should just refresh with one of the view disabled and the position of the ViewPager shouldn't go the last index of the ViewPager.
  • Need to know exactly how and when does the notifyDataSetChanged() works. I did get some info through this STACK_OVERFLOW_POST though.

What I have tried so far :

  • called notifyDataSetChanged() on the onResumeFragment() and called the viewPager.setCurrentPosition(some_index) after that.

  • Tried to Recreate the adapter(Least expected way).

  • I have a swapCursor method in the adapter that I used to switch the cursor when any of the conditions come true.

This is code for SwapCursor :

public void swapCursor(final Cursor c) {
        if (someCursor == c)
            return;
        else
        if(someCursor!=null)
            someCursor.close();

        this.someCursor = c;
        this.someCursor.moveToFirst();
        itemPosition = 0;
        notifyDataSetChanged();

    } 

Any recommendations on how I can do a workaround or maybe a good solution for it. Help is really appreciated. Thanks.

Community
  • 1
  • 1
mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • if you are manipulating the views from the list itself, the notifyDataSetChanged should be called on its own by the viewpager – user2511882 Jun 23 '15 at 21:46
  • @user2511882 I am actually manipulating the views on the Parent class which later notifies those child views that those view values have been changed. Now, if I dont specify the notifydataset changed after I have modified the views, the views wont be modified unless you scroll right to left or vice versa because the initialize method wont know what item is been changed unless the views are scrolled back and forth. – mike20132013 Jun 23 '15 at 22:28
  • why not simply use a interface to communicate with the child views in that case? – user2511882 Jun 23 '15 at 22:48
  • Lol ..of course you can but in the end you still reach point you started. Half the implementation is done with interfaces in this case. But if it was that easy, I wouldn't be confused with this concept. – mike20132013 Jun 23 '15 at 23:31
  • Other way I could possibly do is by passing in my view objects to the parent class but that would just be a quick to the solution and it won't work all the time cause in order to pass in your view objects to the parent class, the view has to to come in the instantiate method and that is called only when you swipe. Overall , it will be a very bad implementation. – mike20132013 Jun 23 '15 at 23:36

0 Answers0