How to pass JSON data from ListView to new activity

ⅰ亾dé卋堺 提交于 2019-12-13 09:59:09

问题


I have created a ListView that is populated with JSON data, and I have included OnItemClickListener to open a new Activity through Intent. What I'm trying to do is to make that new Activity generate data based on the item I have clicked in the ListView.

For example, if I clicked item from JSON field name JohnDoe in the ListView, I want to generate page with information from another JSON data file. And concrete example would be like the Google Play Store.

I click some app and it opens a page with populated info about that app.

Edit: I have tried that passing object link, and it doesnt work. I have getter and setter in a class, then I parse it in my main class.

Example here:

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

回答1:


You can send an object as a parameter to another activity if the object implements Parcelable:

A typical implementation of Parcelable here

Then, to send like parameter, something like this:

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

And in the other activity:

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

Good luck!




回答2:


If you do not like the Parcelable approach or do not know how to do it you can simply pass the JSONObject as a String like described here.

The only thing you have to worry about is retrieving the right JSONObject inside your OnItemClickListener.



来源:https://stackoverflow.com/questions/34152054/how-to-pass-json-data-from-listview-to-new-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!