Java Android-设计问题-我应该使用setTag保存OnClick事件的信息吗?

Java Android-设计问题-我应该使用setTag保存OnClick事件的信息吗?,java,android,gridview,Java,Android,Gridview,我正在做一个小项目,作为Android课程的一部分。 该应用程序应该使用一些网站的API检索最流行的电影,然后在网格视图中显示它们(使用海报作为缩略图)。 单击特定电影时,将启动一个提供详细信息的新活动 我已经走了这么远: 使用网站的API获得了最受欢迎电影海报图像URL的列表 实现了一个扩展ArrayAdapter的适配器,该适配器接受ArrayList作为数据源,并将图像从URL加载到ImageView项中 使用适配器填充GridView 使用以下代码在Gridview上设置侦听器: s:

我正在做一个小项目,作为Android课程的一部分。 该应用程序应该使用一些网站的API检索最流行的电影,然后在网格视图中显示它们(使用海报作为缩略图)。 单击特定电影时,将启动一个提供详细信息的新活动

我已经走了这么远:

  • 使用网站的API获得了最受欢迎电影海报图像URL的列表
  • 实现了一个扩展ArrayAdapter的适配器,该适配器接受ArrayList作为数据源,并将图像从URL加载到ImageView项中
  • 使用适配器填充GridView
  • 使用以下代码在Gridview上设置侦听器:
  • s:

    并将适配器转换为使用
    ArrayList

  • 与选项1相同,但使用setTag存储电影的Id
  • 与选项1相同,但获取所有必需的信息(标题、评级、情节等),并将其存储到MovieItem。在下一个活动中不需要查询
  • 与选项3相同,但使用setTag(MovieItem)。在下一个活动中不需要查询
  • 我是应用程序开发新手,我正在努力以正确的方式完成工作,希望能得到一些帮助

    编辑:
    还想补充一点,如果我在适配器中存储了额外的电影信息,那会不会因为这些信息与该类无关而不合适


    谢谢你的麻烦!:)

    当您第一次请求电影信息列表时,您可以将每部电影的详细信息存储在类似于
    HashMap
    的东西中,其中字符串是电影id,Map是一组用于详细信息的键/值对。然后,当单击onClick时,您将使用该位置来确定单击了哪个电影海报。然后,您将检索所选电影的详细信息HashMap,将其放入一个包中,将其传递给新活动的意图,然后在另一端检索它。所以代码看起来像这样:

    //You would put each set of movie data into a HashMap like this...
    HashMap<String, HashMap<String, String>> movies = new HashMap<>();
    
    HashMap<String, String> details = new HashMap<>();
    details.put("dateReleased", "7.12.2015");
    details.put("rating", "PG-13");
    details.put("title", "Cool Awesome Movie");
    movies.put("12345", details);
    
    //Then when an onClick comes in, you would get the set of 
    //details for the movie that was selected (based on position)
    HashMap<String, String> selectedDetails = movies.get("12345");
    
    //Put the retrieved HashMap of details into a Bundle
    Bundle bundle = new Bundle();
    bundle.putSerializable("movieDetails", selectedDetails);
    
    //Send the bundle over to the new Activity with the intent
    Intent intent = new Intent(this, YourDetailsActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);
    
    当您第一次检索电影列表时,您将执行以下操作:

    //You would put each set of movie data into a HashMap like this...
    HashMap<String, HashMap<String, String>> movies = new HashMap<>();
    
    HashMap<String, String> details = new HashMap<>();
    details.put("dateReleased", "7.12.2015");
    details.put("rating", "PG-13");
    details.put("title", "Cool Awesome Movie");
    movies.put("12345", details);
    
    //Then when an onClick comes in, you would get the set of 
    //details for the movie that was selected (based on position)
    HashMap<String, String> selectedDetails = movies.get("12345");
    
    //Put the retrieved HashMap of details into a Bundle
    Bundle bundle = new Bundle();
    bundle.putSerializable("movieDetails", selectedDetails);
    
    //Send the bundle over to the new Activity with the intent
    Intent intent = new Intent(this, YourDetailsActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);
    
    //您可以将每组电影数据放入如下的哈希映射中。。。
    HashMap movies=新建HashMap();
    HashMap details=新的HashMap();
    详情。出售(“日期发布”,“7.12.2015”);
    详情。出售(“评级”,“第13页”);
    详细信息。放置(“标题”,“酷极了的电影”);
    电影。把(“12345”,细节);
    //然后,当onClick出现时,您将获得
    //所选电影的详细信息(基于位置)
    HashMap selectedDetails=movies.get(“12345”);
    //将检索到的详细信息HashMap放入一个包中
    Bundle=新Bundle();
    bundle.putSerializable(“movieDetails”,selectedDetails);
    //将捆绑包发送到新的活动,并带有
    Intent Intent=新的Intent(这是YourDetailsActivity.class);
    意向。额外支出(捆绑);
    星触觉(意向);
    
    然后,当新活动开始时,您将从onCreate()中的包中检索详细信息:

    //然后在新活动的onCreate()中。。。
    Bundle Bundle=getIntent().getExtras();
    HashMap movieDetails=(HashMap)bundle.getSerializable(“movieDetails”);
    字符串dateReleased=movieDetails.get(“dateReleased”);
    字符串评级=movieDetails.get(“评级”);
    字符串title=movieDetails.get(“title”);
    
    谢谢您的回答!这看起来确实是一个不错的选择,但我想知道如果我使用了setTag,那会不会是对它的滥用?还想补充一点,如果我在适配器中存储了额外的电影信息,那会不会是不合适的,因为这些信息与该类无关?对不起,如果我是一个麻烦,但我正在寻找答案,而不仅仅是'这样做'。谢谢:)当您说要使用setTag()时,我假设您指的是ImageView的setTag()?您当然可以使用它来存储id,然后按照您提到的意图传递它。但是这也意味着您必须发出两个url请求,而不是一个。如果您有少量的数据(比如,一个包含10部电影的列表),我可能会选择一次下载所有数据,然后使用存储的数据创建详细活动。如果你喜欢100部电影,那么每次点击获取详细信息可能会更好。至于你关于在ArrayAdapter中执行所有这些操作的问题,你必须根据自己的喜好做出决定。您当然可以在适配器内部完成所有这些工作,但我想说,大多数开发人员都希望在自己的类中使用这些服务器请求(我知道我会)。保持类和方法尽可能小和集中通常是最佳实践。当涉及到维护自己的代码和跟踪问题时,这总是更好的选择。很抱歉,如果之前不清楚,我指的是ImageView.setTag()。我不打算在适配器中执行服务器请求,而是在设置/更新数据时存储附加信息。请求本身是在它之外完成的。至于你的解决方案,我刚刚意识到我不能使用位置来点击电影,因为外部HashMap的键是实际的电影id,其中As position是一个索引。因此,无论哪种方式,我都必须使用setTag或数组来存储它,并使用position作为索引。不管怎样,非常感谢你的帮助,我想我从现在起就得到了这个
    //You would put each set of movie data into a HashMap like this...
    HashMap<String, HashMap<String, String>> movies = new HashMap<>();
    
    HashMap<String, String> details = new HashMap<>();
    details.put("dateReleased", "7.12.2015");
    details.put("rating", "PG-13");
    details.put("title", "Cool Awesome Movie");
    movies.put("12345", details);
    
    //Then when an onClick comes in, you would get the set of 
    //details for the movie that was selected (based on position)
    HashMap<String, String> selectedDetails = movies.get("12345");
    
    //Put the retrieved HashMap of details into a Bundle
    Bundle bundle = new Bundle();
    bundle.putSerializable("movieDetails", selectedDetails);
    
    //Send the bundle over to the new Activity with the intent
    Intent intent = new Intent(this, YourDetailsActivity.class);
    intent.putExtras(bundle);
    startActivity(intent);
    
     //Then in onCreate() of the new Activity...
     Bundle bundle = getIntent().getExtras();
     HashMap<String, String> movieDetails = (HashMap<String, String>) bundle.getSerializable("movieDetails");
     String dateReleased = movieDetails.get("dateReleased");
     String rating = movieDetails.get("rating");
     String title = movieDetails.get("title");