【问题标题】:How to pass JSON data from ListView to new activity如何将 JSON 数据从 ListView 传递到新活动
【发布时间】:2016-03-13 03:31:23
【问题描述】:

我创建了一个ListView,其中填充了JSON 数据,并且我已经包含OnItemClickListener 以打开一个新的ActivityIntent。我要做的是让新的Activity 根据我在ListView 中单击的项目生成数据。

例如,如果我在ListView 中单击来自JSON 字段名称JohnDoe 的项目,我想生成包含来自另一个JSON 数据文件的信息的页面。具体的例子就像 Google Play 商店。

我单击某个应用程序,它会打开一个页面,其中包含有关该应用程序的填充信息。

编辑:我已经尝试过传递对象链接,但它不起作用。我在一个类中有 getter 和 setter,然后在我的主类中解析它。

这里的例子:

  JSONObject obj = response.getJSONObject(i);
                                    Movie movie = new Movie();
                                    movie.setTitle(obj.getString("title"));

【问题讨论】:

  • 你的方法在哪里??太老的问题
  • 我在参考链接中尝试过,但它不起作用。所以这就是为什么我提出这个问题来进行详细解释。如果您还没有听到我要说的更多内容,您无需投反对票..

标签: android json listview android-listview


【解决方案1】:

如果对象实现Parcelable,您可以将对象作为参数发送到另一个活动:

A typical implementation of Parcelable here

然后,发送类似的参数,像这样:

Intent intent = new Intent(this, DetailMovieActivity.class);
intent.putExtra("key", movie);//Your object that implements Parcelable
startActivity(intent);

在其他活动中:

Bundle arguments = new Bundle();
Movie movie = getIntent().getParcelableExtra("key"));//Receive object

祝你好运!

【讨论】:

    【解决方案2】:

    如果您不喜欢 Parcelable 方法或不知道如何操作,您可以简单地将 JSONObject 作为字符串传递,如 here 所述。

    您唯一需要担心的是在 OnItemClickListener 中检索正确的 JSONObject。

    【讨论】:

      猜你喜欢
      • 2019-08-13
      • 2011-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多