-1

I have tried to create a custom PagerAdapter since I want to show some pages, where each page contains a ListView, that should be initialized by the pager adapter constructor MyPagerAdapter, that receives the Activity where the ViewPager is used, and a list of Strings entries.

The problem is that setAdapter(myListAdapter) called on the ListView seems ineffective and listview appears empty.

Here the piece of code where I try to initialize the ListView:

 public class MyPagerAdapter extends PagerAdapter {
 ...
 public MyPagerAdapter( Activity activity, ArrayList<String> entries){
        mContext = activity.getBaseContext();
        inflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) activity.findViewById(R.id.pager);
        View v=inflater.inflate(R.layout.page1, viewGroup);
        ListView page_1_list = (ListView) v.findViewById(R.id.page_1_list);
        mylistAdapter = new ArrayAdapter(mContext, R.layout.spinner_item, entries);
        page_1_list.setAdapter(mylistAdapter);
 }
  ....
}

Could someone explain me why happens this and how could I fix the error?

  • The entries list isn't empty.
  • The ViewPager has several pages but here I'm showing only the list relative to the first page.
  • The issue has nothing to do with the xml since if I try to fill It directly from the activity works.
  • I'm able to google myself ViewPager tutorials. The question is related to a problem, and the answer should be related to the problem itself. So if your answer doesn't explain WHY listView cannot be filled with items with described approach It is off-topic and I will not accept It.
AndreaF
  • 11,975
  • 27
  • 102
  • 168
  • Why you doing this in constructor?. I think you should go with `Fragment` and [FragmentStatePagerAdapter](https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter). – ADM May 02 '18 at 13:37
  • This is not a way of doing such thing. Does the `ViewPager` contains endless items ? – ADM May 02 '18 at 13:40
  • @ADM This is the Constructor of the PagerAdapter that I use in the Activity where I have the `ViewPager` (that is the `Activity` that is passed as argument in the constructor). – AndreaF May 02 '18 at 13:40
  • 2
    Yeah i know this . But Does the ViewPager contains endless items ? Anyway yes/NO both cases i suggest to go with above solution . And to answer your question your code is not enough . So add complete code . – ADM May 02 '18 at 13:42
  • @ADM read the question more carefully. The code is enough since the issue is related to the way I initialize the list indirectly in the adapter. – AndreaF May 02 '18 at 13:47
  • 1
    what is the view `v` here? Also, the view pager contains fragments right? Why not initialize the lists in the fragment the proper way? – denvercoder9 May 02 '18 at 13:50
  • @rafid059 The v is where I inflate the layout of the single page. – AndreaF May 02 '18 at 13:58

1 Answers1

0

You have to use FragmentStatePagerAdapter instead of directly use controllers in Pageradapter class. i.e:

public class CustomPagerAdapter extends FragmentStatePagerAdapter {

private ArrayList<String> entries;
private MyFragment myFragment;

public CustomPagerAdapter(FragmentManager fragmentManager, ArrayList<String> entries) {
    super(fragmentManager);
    this.entries = entries;
}

// Returns total number of pages
@Override
public int getCount() {
    return entries.size();
}

// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
    myFragment = new MyFragment();
    final Bundle bundle = new Bundle();
   //pass your array list in bundle
    myFragment.setArguments(bundle);
    return myFragment;
}

// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {

    return title_of_viewpager;
}
}
immodi
  • 607
  • 1
  • 6
  • 21