2

I am trying to swipe item(from listview) in my detail activity using viewpager. I have 20 items in my listview (data parsed from json) Instead of swiping the next/previous item , It's only the same item that I swipe (20 times).

here is my DetailActivity.

public class DetailsActivity extends FragmentActivity {


Article feed;

int pos;
private DescAdapter adapter;
private ViewPager pager;
...
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.item_details);

    pos = getIntent().getExtras().getInt("pos");

    adapter = new DescAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.pager);       
    pager.setAdapter(adapter);  
    pager.setCurrentItem(pos);

}

public class DescAdapter extends FragmentStatePagerAdapter {
    public DescAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public int getCount() {
        return 20;
    }

    public Fragment getItem(int position) {
         return DetailFragment.newInstance(position);
     }
}

And my detailFragment

public class DetailFragment extends Fragment {

static DetailFragment newInstance(int position) {
     DetailFragment f = new DetailFragment();        
     return f;
 }

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.detail_fragment, container, false);

    String displaytitle= getActivity().getIntent().getStringExtra("title");
    TextView titleF = (TextView)view.findViewById(R.id.title);

    titleF.setText(displaytitle);  
    ...      
    return view;
} 

What am I doing wrong?

MBrown
  • 31
  • 7

1 Answers1

1

You are not using the correct argument:
You send the argument "pos2" but you retrieve the argument "title".

@Override
public Fragment getItem(int position) {
    DetailFragment frag = new DetailFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("fPos",position);
    frag.setArguments(bundle);
    return frag;
}
MBrown
  • 31
  • 7
Simon Marquis
  • 7,248
  • 1
  • 28
  • 43
  • hi @Simon Marquis thank you for your reply, I edited my post . I still can't swipe to the next item . – MBrown Aug 13 '14 at 19:04
  • Alright, on your code, your are sending the same feed over and over: `bundle.putSerializable("feed", feed);` – Simon Marquis Aug 13 '14 at 19:06
  • Ok I have cleaned up my code, I think something is wrong with my getItem(int position), I tried to google it but can't find a solution. I swipe the same item – MBrown Aug 13 '14 at 20:03
  • No your actually aren't displaying the same. `return DetailFragment.newInstance(position);` this will create a new DetailFragment for each page. BUT you pass the `position` AND you never use it in the newInstance method. You should give it as an argument like you did before with `setArguments` – Simon Marquis Aug 13 '14 at 20:10
  • hi! you were right! I implemented parcalable in my model and passed the position as an argument for my detailFragment . Everything is okay now. Thanks :) – MBrown Aug 23 '14 at 16:59