如果我有一个jsonarray并且我在listview中显示它并想要列表中的specifec项目怎么做?

时间:2016-07-28 13:34:53

标签: android json listview

我想要做的是查看json将所有名称拉到列表视图(保存名称的imdbid)并从那里你可以点击一个电影,它会转到一个新的意图,将带给你您点击的电影及其名称摘要和图像

我在json中搜索一个看起来像这样的数组(基于用户搜索的内容,这是一个例子......)

  {"Search":[{"Title":"Batman     Begins","Year":"2005","imdbID":"tt0372784","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BNTM3OTc0MzM2OV5BMl5BanBnXkFtZTYwNzUwMTI3._V1_SX300.jpg"},{"Title":"Batman v Superman: Dawn of Justice","Year":"2016","imdbID":"tt2975590","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BNTE5NzU3MTYzOF5BMl5BanBnXkFtZTgwNTM5NjQxODE@._V1_SX300.jpg"},{"Title":"Batman","Year":"1989","imdbID":"tt0096895","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BMTYwNjAyODIyMF5BMl5BanBnXkFtZTYwNDMwMDk2._V1_SX300.jpg"},{"Title":"Batman Returns","Year":"1992","imdbID":"tt0103776","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BODM2OTc0Njg2OF5BMl5BanBnXkFtZTgwMDA4NjQxMTE@._V1_SX300.jpg"},{"Title":"Batman Forever","Year":"1995","imdbID":"tt0112462","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BNWY3M2I0YzItNzA1ZS00MzE3LThlYTEtMTg2YjNiOTYzODQ1XkEyXkFqcGdeQXVyMTQxNzMzNDI@._V1_SX300.jpg"},{"Title":"Batman & Robin","Year":"1997","imdbID":"tt0118688","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BNTM1NTIyNjkwM15BMl5BanBnXkFtZTcwODkxOTQxMQ@@._V1_SX300.jpg"},{"Title":"Batman: The Animated Series","Year":"1992–1995","imdbID":"tt0103359","Type":"series","Poster":"http://ia.media-imdb.com/images/M/MV5BMTU3MjcwNzY3NF5BMl5BanBnXkFtZTYwNzA2MTI5._V1_SX300.jpg"},{"Title":"Batman: Under the Red Hood","Year":"2010","imdbID":"tt1569923","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BMTMwNDEyMjExOF5BMl5BanBnXkFtZTcwMzU4MDU0Mw@@._V1_SX300.jpg"},{"Title":"Batman: The Dark Knight Returns, Part 1","Year":"2012","imdbID":"tt2313197","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BMzIxMDkxNDM2M15BMl5BanBnXkFtZTcwMDA5ODY1OQ@@._V1_SX300.jpg"},{"Title":"Batman: Mask of the Phantasm","Year":"1993","imdbID":"tt0106364","Type":"movie","Poster":"http://ia.media-imdb.com/images/M/MV5BMTMzODU0NTYxN15BMl5BanBnXkFtZTcwNDUxNzUyMQ@@._V1_SX300.jpg"}],"totalResults":"310","Response":"True"}

所以我想要做的是当他点击包含电影名称的listview时获取imdbid 这就是我的尝试:

   JSONObject jsonObject = new JSONObject(finalJson);
            JSONArray parentArray = jsonObject.getJSONArray("Search");
            StringBuffer finalStringBuffer = new StringBuffer();
            String imdbid ;
            for (int i=0; i<parentArray.length(); i++){
                JSONObject finalJsonObject = parentArray.getJSONObject(i);
                String movieName = finalJsonObject.getString("Title");
                nameOfMovie.add(movieName);
                String year = finalJsonObject.getString("Year");
                yearOfMovie.add(year);
                String omdbID = finalJsonObject.getString("imdbID");
                id.add(omdbID);
                finalStringBuffer.append(movieName + " , " + year + " , " + omdbID + "\n");
                imdbid = omdbID ;
            }

        }
        catch (JSONException e) {
            e.printStackTrace();
        }

    }

但我怎么知道他所穿的imdbid(在列表中显示的电影名称中)? 列表视图代码:

 listViewInternetScreen.setClickable(true);
    listViewInternetScreen.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position, long i) {
            Intent intenttoEditScreen=new Intent(AddFromInternet.this, EditMovie.class);
            setResult(RESULT_OK,intenttoEditScreen);
            String jsonMovieName = String.valueOf(nameOfMovie);
            String jsonMovieSummery = String.valueOf(yearOfMovie);
            String jsonImageURL = String.valueOf(id);

            intenttoEditScreen.putExtra("json", jsonMovieName);
            intenttoEditScreen.putExtra("json", jsonMovieSummery);
            intenttoEditScreen.putExtra("json", jsonImageURL);
            startActivity(intenttoEditScreen);


        }

        });

和搜索方法:

     btnGo = (Button) findViewById(R.id.btnGo);
    assert btnGo != null;
    btnGo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            //MAKE A SEARCH STRING AND PARSE THE RESULT
            final String search = "http://www.omdbapi.com/?s=" + EtSearch.getText().toString();
            Log.e("JSON", search);
            new JSONParser().execute(search);
            ArrayList<String> list = new ArrayList<>(nameOfMovie);
            arrayAdapter = new ArrayAdapter<>(AddFromInternet.this,
                    android.R.layout.simple_list_item_1, list);
            arrayAdapter.notifyDataSetChanged();
            listViewInternetScreen.setAdapter(arrayAdapter);
            Log.e("JSON MOVIE", String.valueOf(nameOfMovie));

        }
    });

感谢您的帮助:D

1 个答案:

答案 0 :(得分:0)

步骤1:使用Gson(和Retrofit,如果您愿意)简化将JSON字符串转换为Java对象列表

步骤2:创建一个加载此对象列表的自定义ArrayAdapter。然后,当您添加项目单击侦听器时,您可以从单击的列表项中获取该对象,并启动Intent以显示该影片的完整信息。

附加步骤是使Movie对象成为Parcelable,这样就可以在一行中将对象添加到Intent中,而不是将它包含在每个单独的字段中