android - Passing Strings from listview that has not been populated -
i pass string tag_question_content = "content";
string tag_question_subject = "subject";
, string tag_question_chosenanswer = "chosenanswer";
when click on item want, string displays in next activity string tag_question_subject = "subject";
reason because string populating listview. not want other 2 strings populated in listview, want pass them next activity can displayed there. can show me way pass these 2 other strings without populating them.
public class listview extends listactivity { arraylist<hashmap<string, string>> questionlist; final string tag_results = "results"; final string tag_question_subject = "subject"; final string tag_question_numanswers = "numanswers"; final string tag_question = "question"; final string tag_question_content = "content"; final string tag_question_chosenanswer = "chosenanswer"; final string tag_answers = "answers"; final string tag_answer = "answer"; final string tag_answers_content = "content"; final string tag_query = "query"; jsonarray question = null; android.widget.listview lv; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //setcontentview(r.layout.listview); questionlist = new arraylist<hashmap<string, string>>(); new loadalldata().execute(); } @override protected void onlistitemclick(android.widget.listview l, view v, int pos, long id) { super.onlistitemclick(l, v, pos, id); string subject = ((textview) v.findviewbyid(r.id.subject)).gettext().tostring(); string content = ((textview) v.findviewbyid(r.id.content)).gettext().tostring(); string chosenanswer = ((textview) v.findviewbyid(r.id.chosenanswer)).gettext().tostring(); intent = new intent(listview.this, singlelistitem.class); i.putextra(tag_question_subject, subject); i.putextra(tag_question_content, content); i.putextra(tag_question_chosenanswer, chosenanswer); startactivity(i); } class loadalldata extends asynctask<string, string, string> { private dialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); progressdialog pdialog; pdialog = new progressdialog(listview.this); pdialog.setmessage("loading data. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); if (pdialog != null && pdialog.isshowing()) pdialog.dismiss(); } protected string doinbackground(string... args) { try { intent in = getintent(); string searchterm = in.getstringextra("tag_search"); string query = urlencoder.encode(searchterm, "utf-8"); string url = "http://example.com"; jsonparsser jparser = new jsonparsser(); jsonobject json = jparser.readjsonfeed(url); try { jsonarray questions = json.getjsonobject("all").getjsonarray("questions"); for(int = 0; < questions.length(); i++) { jsonobject question = questions.getjsonobject(i); string subject = question.getstring(tag_question_subject); string numanswers = question.getstring(tag_question_numanswers); string chosenanswer = question.getstring(tag_question_chosenanswer); string content = question.getstring(tag_question_content); //jsonarray answers = question.getjsonobject(tag_answers).getjsonarray(tag_answer); //jsonobject answer = answers.getjsonobject(0); //string content = answer.getstring(tag_answers_content); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_question_subject, subject); map.put(tag_question_numanswers, numanswers); map.put(tag_question_content, content); map.put(tag_question_chosenanswer, chosenanswer); questionlist.add(map); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } } catch (unsupportedencodingexception e) { // todo auto-generated catch block e.printstacktrace(); } return tag_question ; } @override protected void onpostexecute(string file_url) { listadapter adapter = new simpleadapter(getbasecontext(), questionlist, r.layout.listelements, new string[] { tag_question_subject, tag_question_numanswers }, new int[] { r.id.subject, r.id.numanswers, }); setlistadapter(adapter); }}}
singlelistitem activity:
public class singlelistitem extends activity { textview title; textview question; textview bestanswer; textview subject; textview content; textview chosenanswer; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setcontentview(r.layout.singlelistitem); title = (textview) findviewbyid(r.id.title1); question = (textview) findviewbyid(r.id.question1); bestanswer = (textview) findviewbyid(r.id.bestanswer1); subject = (textview) findviewbyid(r.id.subject1); content = (textview) findviewbyid(r.id.content1); chosenanswer = (textview) findviewbyid(r.id.chosenanswer1); intent = getintent(); string subject = i.getstringextra("subject"); string content = i.getstringextra("content"); string chosenanswer = i.getstringextra("chosenanswer"); subject.settext(subject); content.settext(content); chosenanswer.settext(chosenanswer); } }
you need use data model in such cases. code below should work you.
@override protected void onlistitemclick(android.widget.listview l, view v, int pos, long id) { super.onlistitemclick(l, v, pos, id); hashmap<string, string> item = questionlist.get(pos); intent = new intent(listview.this, singlelistitem.class); i.putextra(tag_question_subject, item.get(tag_question_subject)); i.putextra(tag_question_content, item.get(tag_question_content)); i.putextra(tag_question_chosenanswer, item.get(tag_question_chosenanswer)); startactivity(i); }