OnClick 打开 URL (Json, ListView) Android
Posted
技术标签:
【中文标题】OnClick 打开 URL (Json, ListView) Android【英文标题】:OnClick open URL (Json, ListView) Android 【发布时间】:2015-10-30 01:33:34 【问题描述】:你好,我在 json 中有一个项目列表(listview),代码如下:
listView = (ListView) v.findViewById(R.id.list);
adapter = new CustomListAdapter(getActivity(), movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>()
@Override
public void onResponse(JSONArray response)
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++)
try
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++)
genre.add((String) genreArry.get(j));
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
catch (JSONException e)
e.printStackTrace();
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
);
这是 Json 文件:
"title": "Dawn of the Planet of the Apes",
"image": "http://api.androidhive.info/json/movies/1.jpg",
"rating": 8.3,
"releaseYear": 2014,
"genre": ["Action", "Drama", "Sci-Fi"]
,
"title": "District 9",
"image": "http://api.androidhive.info/json/movies/2.jpg",
"rating": 8,
"releaseYear": 2009,
"genre": ["Action", "Sci-Fi", "Thriller"]
,
"title": "Transformers: Age of Extinction",
"image": "http://api.androidhive.info/json/movies/3.jpg",
"rating": 6.3,
"releaseYear": 2014,
"genre": ["Action", "Adventure", "Sci-Fi"]
,
"title": "X-Men: Days of Future Past",
"image": "http://api.androidhive.info/json/movies/4.jpg",
"rating": 8.4,
"releaseYear": 2014,
"genre": ["Action", "Sci-Fi", "Thriller"]
,
我想在 json 的每个项目中添加一个链接,例如 "url": "http://google.com" 并且应用程序打开链接 (Action_VIEW)。对不起我的英语。
【问题讨论】:
【参考方案1】:请查看:
for (int i = 0; i < response.length(); i++)
try
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
movie.setUrlLink("http://google.com");
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++)
genre.add((String) genreArry.get(j));
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);
对于 Action View:将这些代码放在 Click Listner 中:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(movie.getUrlLink())));
注意:我想你知道如何从 bean 中获取数据。
【讨论】:
您好,可以联系您吗?你的邮件是什么?以上是关于OnClick 打开 URL (Json, ListView) Android的主要内容,如果未能解决你的问题,请参考以下文章
带有onclick的android imagebuttons上的JSON数组响应
嵌套关系是不是应该反映在 JSON API 的 URL 中?