如何通过活动(主从流程)之间的大集合 [英] How to pass a large collection between Activities (Master-Detail flow)

查看:142
本文介绍了如何通过活动(主从流程)之间的大集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我实现一个应用程序,读取关于从Web服务的电影信息。该Web服务返回几个信息关于每部电影(标题,日期,URL海报,导演,演员等)。

I'm implementing an app that reads info about movies from a web service. That web service return several info about each movie (title,date,poster url, director, actors, etc).

这是Web服务支持分页,所以电影中的100包装。

That web service supports pagination, so the movies are loaded in packs of 100.

执行:

的想法是要表明一个网格与所有的海报。自动请求更多的项目,当用户向下滚动。

The idea is to show a grid with all the posters. Auto requesting more items when the user scrolls down.

输入图像的描述在这里

当一个项目被点击的用户浏览到与所选影片的详细视图画廊,允许通过与ViewPager细节滚动。

When an item is clicked the user navigates to a gallery with the detail view of the selected movie, allowing the scroll through the details with a ViewPager.

输入图像的描述在这里

这样的想法是通过检索在网格的DetailedGalleryActivity电影的集合

So the idea is to pass the collection of movies retrieved in the grid to the "DetailedGalleryActivity".

更新:也有必要在用户离开片段,以便处理该片段的生命周期时,保存的状态。您可以测试它使开发人员选项:不保留活动

UPDATE: Also is necessary to save the state when the user leaves the fragment in order to handle the fragment lifecycle. You can test it enabling the Developer option: Don't keep activities

问题

我的第一种方法是序列化的电影集合在一个JSON,并将其作为一个String额外的活动。

My first approach was to serialize the collection of movies in a json, and pass it as a String extra to the Activity.

不过,由于电影列表大,如果用户滚动了很多的网格,JSON的大小是非常大的一个包(见<一href="http://stackoverflow.com/questions/28729955/max-size-of-string-data-that-can-be-passed-in-intents">Max字符串数据的大小),获得运行时异常。

But since the list of movies is big, if the user scrolls a lot in the grid, the size of the json is extremely big for a Bundle (see Max size of string data), getting runtime exceptions.

我也查了一些答案,开展具体活动之前谈坚持共享preferences或其他永久存储的数据,然后从细节访问它。我觉得这个解决方案奇怪,因为它忽略了一个机制,通过活动之​​间的数据与自定义和手工制作的解决方案。

I have checked some answers that talks about persist the data in the SharedPreferences or other persistent storage before launching the detail activity and then access to it from the detail. I find this solution weird because it ignores the mechanisms to pass data between activities with a custom and handmade solution.

什么是解决这个问题的最好办法?

What is the best approach to solve this problem?

推荐答案

我不也知道这是一个很好的建议。

I am not also sure if this is a good suggestion.

请另一个类,并添加一个静态方法,让我们将其命名为MovieUtil。

Make another class and add a static method let's name it MovieUtil.

//样品

private class MovieUtil {
  private static List<Movies> movies;
  public static List<Movies> getMovies();//getter
  public static void setMovies(List<Movie> movies);//setter
}

在你开始DetailedGalleryActivity集电影对MovieUtil.setMovies)列表(

Before you start the DetailedGalleryActivity set the list of movies on MovieUtil.setMovies()

然后在OnCreate DetailedGalleryActivity的使用MovieUtil.getMovies()初始化列表

then on oncreate of DetailedGalleryActivity initialize the List by using the MovieUtil.getMovies()

请注意:你可以清除movieutil电影列表,当你关闭DetailedGalleryActivity

Note: you can clear the list of movies on movieutil when you close the DetailedGalleryActivity.

这篇关于如何通过活动(主从流程)之间的大集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆