根据JSON密钥动态地将选项卡添加到ViewPager

时间:2014-07-19 19:45:08

标签: java android json android-fragments android-viewpager

我在JSON中获取不同事件的JSON数据,并根据格式(下面附带的格式),我根据每个event中的键创建了一个event类。然后我将所有events数据放入events ArrayList,同时我解析JSON,稍后我在ListView中使用自定义适配器展示BaseAdapter - 一切正常精细化在这里,事件在ListView中显示。

但我现在要做的就是让ViewPager和ViewPager中的Tabs根据日期动态生成。就好如果Schedule中有3个不同的日期(某些事件发生在一个日期而其他事件发生在其他日期),ViewPager会获得3个Tabs,并且在每个选项卡中,仅ListView所示用户的date所示事件通过选择该日期选项卡可以查看特定日期的事件。不幸的是,我一直困在它上面而无法实现它。

请指导和帮助我,我应该如何继续实现这一点(即根据不同的日期动态生成标签并仅显示相应的事件)?

p.s事件JSON有一个键[ { "title": "Opening", "subtitle": "might be there", "start": "13:30", "end": "14:30", "location": "B01.063", "date": "2014-08-01", "authors": ["George Martin"], "type": "talk", "abstract": "http://..." }, { "title": "Coffe break", "subtitle": "might be there", "start": "16:15", "end": "17:00", "location": "Crown room", "date": "2014-08-02", "authors": [], "type": "break", "abstract": "http://..." } ]

提前致谢:)


我解析以获取日期的JSON for Schedule:

public class EventScheduleItem {

private String title;

private String subtitle;

private String start;

private String end;

private String location;

private String date;

private String authors;

private String type;

private String eventAbstract;

//here are getter/setters and constructor for this class

}

活动类:

List<EventScheduleItem> eventsRecordsArray = new ArrayList<EventScheduleItem>() ;

这就是我创建Events Arraylist的方式:

private class CustomPagerAdapter extends FragmentStatePagerAdapter { public CustomPagerAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public Fragment getItem(int position) { //Something should go there which I don't know what should i do to get events for the date } @Override public int getCount() { return eventsarray.length; //This too i guess is wrong since Tabs coun't would be based on different dates } }

修改

{{1}}

1 个答案:

答案 0 :(得分:0)

在您的活动中尝试此操作:

String[] tabs = {"Tab 1", "Tab 2", "Tab 3"};
mAdapter = new CustomPagerAdapter(getSupportFragmentManager(), tabs);

并在您的寻呼机适配器

private class CustomPagerAdapter extends FragmentStatePagerAdapter {

    String[] tabs;
    public CustomPagerAdapter(FragmentManager fragmentManager, String[] tabs) {
        super(fragmentManager);
        this.tabs = tabs;
    }

    @Override
    public Fragment getItem(int position) {
        //Something should go there which I don't know what should i do to get events for the date
    }

    @Override
    public int getCount() {
    return tabs.length();
    }

    @Override
    public CharSequence getPageTitle(int position){
        return tabs[position];
    }
}