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.