android - ListView Item to webView in Main Activity -
what trying load url in webview in main activity populated listview item. not sure how , learning step step, working way adapt populating listview database once learn step (in question).
the geturl variable needs used load link mwebview in .main activity when list item object clicked. (make sense??) thanx help!!
below have far:
the listview activity (aclist.java):
public class aclist extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.ac_list); arraylist<searchresult> searchresults = getsearchresults(); final listview lv1 = (listview) findviewbyid(r.id.listitems); lv1.setadapter(new mycustombaseadapter(this, searchresults)); lv1.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> a, view v, int position, long id) { object o = lv1.getitematposition(position); searchresult fullobject = (searchresult)o; toast.maketext(aclist.this, "you have chosen: " + " " + fullobject.getlabel(), toast.length_long).show(); /** -- here class intend start specified url selected item intent = new intent(aclist.this, main.class); i.putextra("url", fullobject.geturl()); startactivity(i); **/ } }); } private arraylist<searchresult> getsearchresults() { arraylist<searchresult> results = new arraylist<searchresult>(); searchresult sr1 = new searchresult(); sr1.setlabel("the lable"); sr1.setlisttitle("this title"); sr1.setcaption("this about"); sr1.seturl("http://www.somesite.com/index.html"); results.add(sr1); } }
adapter (mycustombaseadapter):
public class mycustombaseadapter extends baseadapter { private static arraylist<searchresult> searcharraylist; private layoutinflater minflater; public mycustombaseadapter(context context, arraylist<searchresult> results) { searcharraylist = results; minflater = layoutinflater.from(context); } public int getcount() { return searcharraylist.size(); } public object getitem(int position) { return searcharraylist.get(position); } public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.list_item, null); holder = new viewholder(); holder.txtlabel = (textview) convertview.findviewbyid(r.id.label); holder.txtlisttitle = (textview) convertview.findviewbyid(r.id.listtitle); holder.txtcaption = (textview) convertview.findviewbyid(r.id.caption); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.txtlabel.settext(searcharraylist.get(position).getlabel()); holder.txtlisttitle.settext(searcharraylist.get(position).getlisttitle()); holder.txtcaption.settext(searcharraylist.get(position).getcaption()); return convertview; } static class viewholder { textview txtlabel; textview txtlisttitle; textview txtcaption; } }
...and searchresult.java:
public class searchresult { private string label = ""; private string listtitle = ""; private string caption = ""; private string listurl = ""; public void setlabel(string label) { this.label = label; } public string getlabel() { return label; } public void setlisttitle(string listtitle) { this.listtitle = listtitle; } public string getlisttitle() { return listtitle; } public void setcaption(string caption) { this.caption = caption; } public string getcaption() { return caption; } public void seturl(string listurl) { this.listurl = listurl; } public string geturl() { return listurl; } }
i figured out problem was; missing string variable in .main activity links w/the item in listview activity. see below (first line).
bonus - whoever has use it: onprogresschanged in webchromeclient shown below has pre-loader fires every time new url loaded it. add "pd" variable below setcontent(r.layout.myactivity) , copy & past mwebview.setwebclient block in2 activity has webview. enjoy!
.... setcontentview(r.layout.main); activitytitle = (textview) findviewbyid(r.id.app_name); activityicon = (imagebutton) findviewbyid(r.id.icon); pd = progressdialog.show(this, "", "initializing....", true); ..... string url =getintent().getstringextra("url");// <-- gets url passed list activity mwebview= (webview) findviewbyid(r.id.webview1); mwebview.getsettings().setjavascriptenabled(true); mwebview.loadurl(url); mwebview.setwebchromeclient(new webchromeclient() { public void onprogresschanged(webview view, int progress) { activitytitle.settext("loading...."); activity.setprogress(progress * 100); pd.show(); if(progress == 100) pd.dismiss(); activitytitle.settext(" flydroid elua"); activityicon.setimageresource(r.drawable.ic_menu_icon); } });
Comments
Post a Comment