title: Save ListView selected Item in a Fragment having a ViewPager
body: <p>I am trying to make some kind of test app in which you can change from one question to another swiping left or right. So, I have a FragmentActivity, a ViewPager and a FragmentPagerAdapter.</p>
  
  <p>Each new page the FragmentPagerAdapter instantiates a new Fragment using the same Layout which has a ListView on it, up to 5 total pages. Then an ArrayAdapter fills the ListView with 4 CheckedTextViews, when the user selects one, the background changes.</p>
  
  <p>The problem is that, when you swipe from one question to another, the selected item is lost, and when the user swipes back to the previous question the item is no longer selected.</p>
  
  <p>I am newbie with fragments, but when I do the same using checkboxes directly inflated from the xml, their selected state isn't lost when swiping.</p>
  
  <p>Here is some code:</p>
  
  <p><strong>FragmentActivity onCreate():</strong></p>
  
  <pre><code>    @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_test);
          //Getting the array of questions.
          res = getResources();
          questions = res.getStringArray(R.array.questions_1);
  
          // Create the adapter that will return a fragment for each of the five
          // questions of the app.
          mAdapter = new MyAdapter(getSupportFragmentManager());
  
          // Set up the ViewPager with the FragmentPagerAdapter.
          mViewPager = (ViewPager) findViewById(R.id.pager);
          mViewPager.setOffscreenPageLimit(10);
          mViewPager.setAdapter(mAdapter);
      }
  </code></pre>
  
  <p><strong>FragmentPagerAdapter:</strong></p>
  
  <pre><code>    public class MyAdapter extends FragmentPagerAdapter {
          public MyAdapter(FragmentManager fm) {
              super(fm);
  
      }
  
      @Override
      public int getCount() {
              //Number of pages
      return 5;
      }
  
      @Override
      public Fragment getItem(int position) {
          ArrayListFragment fragment = ArrayListFragment
                  .newInstance(position);
  
          return fragment;
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          Locale l = Locale.getDefault();
  
          return getString(R.string.title_section) + " " + (position + 1);
  
      }
  }
  </code></pre>
  
  <p><strong>Fragment Class:</strong></p>
  
  <pre><code>public static class ArrayListFragment extends Fragment {
              //Current Page number.
      int mNum;
      public static final String ARG_SECTION_NUMBER = "section_number";
      ListView list;
  
      /**
       * Create a new instance of ArrayListFragment, providing "num" as an
       * argument.
       */
      static ArrayListFragment newInstance(int num) {
          ArrayListFragment f = new ArrayListFragment();
  
          // Supply num input as an argument.
          Bundle args = new Bundle();
          args.putInt("num", num);
          f.setArguments(args);
  
          return f;
      }
  
      /**
       * When creating, retrieve this instance's number from its arguments.
       */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mNum = getArguments() != null ? getArguments().getInt("num") : 1;
  
      }
  
      /**
       * The Fragment's UI is a textview showing a question and the ListView with               
               * the four possible answers.
       */
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View v = inflater.inflate(R.layout.fragment_pager_list, container,
                  false);
          View tv = v.findViewById(R.id.text);
                      //Set the Question
          ((TextView) tv).setText(questions[mNum]);
          //Get que ListView and fill it.
          list = (ListView) v.findViewById(R.id.listView1);
          list.setAdapter(new ArrayAdapter&lt;String&gt;(getActivity(),
                  R.layout.list_item, myStringArray));
                      //Select an item.
          list.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                      int arg2, long arg3) {
                  // TODO Auto-generated method stub
                  arg1.setSelected(true);
              }
  
          });
  
  
          return v;
      }
  
  
  }
  </code></pre>