0
votes

so I have run now with this problem when trying to use FragmentStatePagerAdapter with the Loader API but no values are set in the UI I mean when the onLoadFinished() is done and set the values to the TextView, ImageView and changeCursor to my Adapter it does not reflect it, I see the UI as I set it in XML.

Let's see if I can explained the issue clearly.

I have a list and when a item is tap will get you to a detail view of this. When an item is selected two elements are sent to the detail, ids array and the position of the item selected.

listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(ActionConstant.DETAIL);

                intent.putExtra("ids", ids);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });

So in the DetailActivity there is a ViewPager which has a FragmentStatePagerAdapter that uses the ids and the position to load the proper data.

@Override
    public void onCreate(Bundle savedIntanceState) {

        super.onCreate(savedIntanceState);
        setContentView(R.layout.detail);
        ids = getIntent().getLongArrayExtra("ids");
        int currentIndex = getIntent().getIntExtra("position",-1);

        mPagerAdapter = new DetailPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager, attaching the adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);

        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentIndex);

    }

And the DetailPagerAdapter override getItem and getCount (which return ids.length)

        @Override
        public Fragment getItem(int i) {
            return DetailFragment.newInstance(ids[i]);
        }

Now in the DetailFragment the newInstance does this

public static DetailFragment newInstance(long id) { DetailFragment f = new DetailFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putLong("_id", id);
    f.setArguments(args);

    return f;
}

and When onLoadFinished is call.

@Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

        if (loader.getId() == 1)
            adapter.changeCursor(cursor);
        else if (loader.getId() == 2) {
            if (!cursor.moveToNext())
                return;

            textName.setText(cursor.getString(cursor.getColumnIndexOrThrow(TraderColumns.NAME)));


    }
}
1

1 Answers

0
votes

Eureka, What was happening was fairly trivial and makes a lots of sense, I had in my onResume() of the Fragment

getActivity().getSupportLoaderManager().restartLoader(CONTACT_REQUEST, null, this);

getActivity().getSupportLoaderManager().restartLoader(TRADER_DETAIL_REQUEST, null, this);

So this would use the supportLoader manager of the activity and since is one instance it got all my views mess up. instead I use:

getLoaderManager().restartLoader(CONTACT_REQUEST, null, this);

getLoaderManager().restartLoader(TRADER_DETAIL_REQUEST, null, this);