android - if i have an jsonarray and i display it in listview and want a specifec item from the list how to do it? -
what im trying @ json pull names list view(save imdbid of names) , there can click on movie , go new intent bring movie clicked on name summary , image
im searching in json array in looks this(it based on user searched 1 example...)
{"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: animated series","year":"1992–1995","imdbid":"tt0103359","type":"series","poster":"http://ia.media-imdb.com/images/m/mv5bmtu3mjcwnzy3nf5bml5banbnxkftztywnza2mti5._v1_sx300.jpg"},{"title":"batman: under red hood","year":"2010","imdbid":"tt1569923","type":"movie","poster":"http://ia.media-imdb.com/images/m/mv5bmtmwndeymjexof5bml5banbnxkftztcwmzu4mdu0mw@@._v1_sx300.jpg"},{"title":"batman: 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 phantasm","year":"1993","imdbid":"tt0106364","type":"movie","poster":"http://ia.media-imdb.com/images/m/mv5bmtmzodu0ntyxn15bml5banbnxkftztcwnduxnzuymq@@._v1_sx300.jpg"}],"totalresults":"310","response":"true"}
so want imdbid when clicks on listview contains movie name tried:
jsonobject jsonobject = new jsonobject(finaljson); jsonarray parentarray = jsonobject.getjsonarray("search"); stringbuffer finalstringbuffer = new stringbuffer(); string imdbid ; (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(); } }
but how can know imdbid(which in movie name displayed in list) cliked on?
listview code:
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); } });
and search method:
btngo = (button) findviewbyid(r.id.btngo); assert btngo != null; btngo.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //make search string , parse 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)); } });
thanks :d
step 1: use gson (and retrofit, if you'd like) simplify turning json string list of java objects
step 2: make custom arrayadapter loads list of objects. then, when add item click listener, can object list item clicked, , start intent showing full info movie.
additional step make movie object parcelable, can add object intent in 1 line, rather each individual field contains
Comments
Post a Comment