我正在使用retrofit从我的网站请求JSON数据。以下是onCreate()
类中的MainActivity
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(BASE_URL)
.build();
ApiEndpointInterface apiService = restAdapter.create(ApiEndpointInterface.class);
apiService.getFeed(9, new Callback<Post>() {
@Override
public void success(Post post, Response response) {
// do something
}
@Override
public void failure(RetrofitError retrofitError) {
retrofitError.printStackTrace();
}
});
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
}
JSON响应如下所示:
[
{
"id":"1",
"user_id":"1",
... more data ...
},
{
"id":"2",
"user_id":"1",
... more data ...
},
... etc ...
]
我想在Fragment中创建一个List,其中每个List项都由返回的JSON中的数据填充。
如何创建List模板,然后用数据填充它?