5

In the class FragmentParentNewsDetail.java,I have a ViewPager,then I use FragmentPagerAdapter to set each page the same Fragment,like this:

@Override
    public Fragment getItem(int position)
    {
        String newsId = this.myNewsIds.get(position);
        Bundle bundle = new Bundle();
        bundle.putInt(FragmentNewsDetail.NEWS_ID, Integer.parseInt(newsId));
        FragmentNewsDetail fragmentNewsDetail = new FragmentNewsDetail();
        fragmentNewsDetail.setArguments(bundle);
        return fragmentNewsDetail;
    }

in the FragmentNewsDetail.java,I have a public method:

public String getNewsTitle()
{
    Log.d(TAG, "getNewsTitle:" + newsTitle);
    return newsTitle;
}

then I want to call the getNewsTitle() method in FragmentNewsDetail.java like this:

 newsTitle = ((FragmentNewsDetail)myAdapter.getItem(vpViewPager.getCurrentItem())).getNewsTitle(); 

But getNewsTitle() always return "",I am confused about that.

Fixed by below method:

public Fragment getCurrentFragment(ViewPager pager,
        FragmentPagerAdapter adapter)
{
    try
    {
        Method m = adapter
                .getClass()
                .getSuperclass()
                .getDeclaredMethod("makeFragmentName", int.class,
                        long.class);
        Field f = adapter.getClass().getSuperclass()
                .getDeclaredField("mFragmentManager");
        f.setAccessible(true);
        FragmentManager fm = (FragmentManager) f.get(adapter);
        m.setAccessible(true);
        String tag = null;
        tag = (String) m.invoke(null, pager.getId(),
                (long) pager.getCurrentItem());
        return fm.findFragmentByTag(tag);
    }
    catch (NoSuchMethodException e)
    {
        e.printStackTrace();
    }
    catch (IllegalArgumentException e)
    {
        e.printStackTrace();
    }
    catch (IllegalAccessException e)
    {
        e.printStackTrace();
    }
    catch (InvocationTargetException e)
    {
        e.printStackTrace();
    }
    catch (NoSuchFieldException e)
    {
        e.printStackTrace();
    }
    return null;
}

from:How to get existing fragments when using FragmentPagerAdapter

Thanks very much!

Community
  • 1
  • 1
MichaelYe
  • 386
  • 4
  • 15
  • You set the extra components required in the Adapter(through Bundle), but did you get the extra component from FragmentNewsDetail.java( via onCreateView())? – Dave Agaba May 16 '13 at 07:14
  • Yes,I did.I get the NEWS_ID in the FragmentNewsDetail(via onCreateView()),then I request the news via the NEWS_ID,but I don't know why the variable newsTitle in the FragmentNewsDetail.java always return "" while I call the method getNewsTitle() from FragmentParentNewsDetail.java. – MichaelYe May 16 '13 at 07:36
  • How did you get the data from the bundle through on createView()? In your case this would be `Bundle data = getArguments();` There after the data which you parsed in your adapter can be got by `int id = data.getIng(FragmentNewsDetails.NEWS_ID);` That should fix it! – Dave Agaba May 18 '13 at 08:05
  • @DaveAgaba hi,thanks for your reply,I fixed it! My solution is in the bottom of my question.Thank you again! – MichaelYe May 27 '13 at 06:28

0 Answers0