Search code examples
androidandroid-viewpagerandroid-pageradapter

fragmentStatePagerAdapter correct data but incorrect adapter on my Fragment


I have a ListFragment to show a list of data based on a specific day. This day is passed as an argument from the getItem() function of my fragmentStatePagerAdapter.

    public Fragment getItem(int position) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, -(mNumDays - (position + 1)));

        return MyFragment.initNewInstance(calendar.getTimeInMillis(), 
                                          position, 
                                          mNumDays);
    }

I also have a LoaderManager to load the data when a change on my database is issued.

On the onCreate() function of my ListFragment I get the day and on onCreateLoader I query the database based on that specific day.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.mDateToShow = getArguments() != null ? getArguments().getString("date") : "";
}

public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
    String sameDay =  ChronomasterDatabaseHelper.KEY_DATE_TIME +"='"+ mDateToShow +"'";

    return new CursorLoader(getActivity(), uri, null, sameDay, null, null);
}

On onLoadFinished() fucntion I start a new thread to get the data and update the ArrayList of my BaseAdapter which is responsible of showing the data in a specific form.

public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
    if ((cursor == null) || (cursor.isClosed())) {
        return;
    }
    try {
        new UpdateDataHelper(cursor).executeOnExecutor();
    }catch (RejectedExecutionException rejectedExecutionException){
        Log.d(LOGTAG,"Task to update data was rejected");
    }
}

And on the thread:

protected void onPostExecute(ArrayList<Application> result) {
    mAdapter.setData(result);
    super.onPostExecute(result);
}

However, from debugging I can see that the data showed to the device are correct(correct data on correct date) but the actual data are incorrect!

What I mean by that?

When I swipe to the page the data queried are of the next day's data (the specific day is actually the next day), and my BaseAdapter has next's day's data. I know that the fragmentStatePagerAdapter by default, not only the visible fragment is loaded, but also the next one and previous one.

How Can I resolve this issue?

When I click on an item on the screen(onListItemClick) I get index Out Of Bounds Exception because my BaseAdapter's ArrayList has the next day's data.

@Override
public void onListItemClick(ListView listView, View view, int pos, long id)
{
    appClicked(mAdapter.getItem(pos - 1));
}

Solution

  • The solution was to have a private ArrayList on my Fragment and add the data there.

    private ArrayList<Application> mResults = new ArrayList<>();

    Then on my thread add the data on my field (Not creating new ArrayList each time):

    mResults.add(new Application(data1,data2,data3));
    mAdapter.setData(mResults,totalDuration,totalTimes);
    

    and onListItemClick

    public void onListItemClick(ListView listView, View view, int pos, long id)
    {
        appClicked(mResults.get(pos - 1));
    }