0

I'm fairly new to Android (and SO) and have made a couple of apps steadily increasing in complexity. For the current project, my son (7) has a aspirations of being a palaeontologist and has asked me to digitise his dinosaur encyclopedia, so I want to have a ViewPager to swipe between the images of dinosaurs which will be clickable for further information.

In my other apps I have used ListViews with a custom datasource (returning a ListArray) so that I can use something like (simplified)...

Item Class:

public class Item {

    private String name;

    public Item(String name) {
        this.name = name;
    }

    public  String getName() {
        return name;
    }
}

Adapter Class:

public class Adapter extends ArrayAdapter<Item> {

    public Adapter(Context context) {
        super(context, 0);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        }

        Item item = getItem(position);

        //Show Name
        TextView listItem_textView = (TextView) convertView.findViewById(R.id.textView);
        listItem_textView.setText(item.getName());

        return convertView;
    }
}

DataSource Class:

public class DataSource {

    private List<Item> items;

    public DataSource() {
        items = new ArrayList<>();

        items.add(new Item("Name_1"));
        items.add(new Item("Name_2"));
        items.add(new Item("Name_3"));
    }

    public List<Item> getItems() {
        return items;
    }
}

...and the MainActivity class doing all the normal things like setting the adapter etc.

Using a ViewPager and Fragments, my current Adapter is like this:

public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

        public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {

//Something needs to go here to mirror
// Item item = getItem(position)
// so that I can use 
// Dino dino = ???

            TextView tv_name = (TextView) findViewById(R.id.tv_name);
            tv_name.setText(dino.getName());

            if(dino.getName() == null) {
                tv_name.setText("Blank");
            }

            return new ScreenSlidePageFragment();
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

The problem - I think - I'm having, is that the Adapter class for my ListView extends ArrayAdapter while the current ViewPager project extends FragmentStatePagerAdapter so I have an 'Incompatible Type' error with 'Item item = getItem(position)' line.

The way I see it, if my understanding is correct, is that I need to get the code for the item position in with the setText() code, other than that, I'm going round in circles, so any pointers would be greatly appreciated.

Thanks for your time.

  • I think you are looking for this : https://www.bignerdranch.com/blog/viewpager-without-fragments/ – dex Sep 05 '16 at 20:27
  • I don't mind using fragments (got to learn somehow, ay!), but that does look interesting, thanks! – Richard KeMiKaL GeNeRaL Denton Sep 05 '16 at 20:29
  • if you are using fragments that's awesome and code is much easier and cleaner :) give it a try ! Rest stackoverflow is there to help you out :) – dex Sep 05 '16 at 20:32
  • The `ViewPager` also accepts an `Adapter`. What you are looking for is the `FragmentPagerAdapter` or `FragmentStatePagerAdapter`. – Xaver Kapeller Sep 05 '16 at 20:33
  • @dex Haha, sorry, when I said I don't mind using them, i mean trying to learn. In fact, this is what i'm trying to do, but I don't know where the above-type code would go? – Richard KeMiKaL GeNeRaL Denton Sep 05 '16 at 20:35
  • I have updated my question to include the sample ViewPager/Fragment code I have been using from the Android.Dev site. – Richard KeMiKaL GeNeRaL Denton Sep 05 '16 at 20:41
  • @RichardKeMiKaLGeNeRaLDenton Lots of tutorial related to viewpager + fragment pager adapter present in internet. inShort your viewpager will contains all your fragments and every fragment will contains all your data but seeing your requirement I would say use pageradapter instead of fragmentpager adapter – dex Sep 05 '16 at 20:42
  • Check this http://stackoverflow.com/questions/18747975/difference-between-fragmentpageradapter-and-fragmentstatepageradapter to understand which pager adapter to use. – masp Sep 06 '16 at 02:07

0 Answers0