android - AsyncTask implementation issues -
can me implementing asynktask code?
i´m new android , don´t know how structure code
public class displayactivity extends listactivity { private final string tag="links"; private arrayadapter<string> madapter = null; arraylist<string> listitems=new arraylist<string>(); arrayadapter<string> adapter; string sites[]={"http://www.dn.pt/inicio", "http://www.expresso.sapo.pt","http://www.publico.pt/?fullsite=true"}; @suppresslint( "newapi") @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_display); // make sure we're running on honeycomb or higher use actionbar apis //if (build.version.sdk_int >= build.version_codes.honeycomb) { // show button in action bar. // getactionbar().setdisplayhomeasupenabled(true); //} final intent intent = getintent(); final string url_link =intent.getstringextra(mainactivity.extra_inputurl); final string keyword = intent.getstringextra(mainactivity.extra_inputkey); madapter=new arrayadapter<string>(this,android.r.layout.simple_list_item_1, android.r.id.text1); final listview list=(listview) findviewbyid(android.r.id.list); list.setadapter(madapter); //get juice keyword.tolowercase(); for(int i=0; < sites.length;i++){ elements elem= gethtmlcode(sites[i]); for(element link:elem){ if(/*link.absurl(sites[i]).contains(link.attr("abs:href")) &&*/ link.text().tolowercase().contains(keyword)){ madapter.add(link.text() + ": " + link.attr("abs:href")); madapter.notifydatasetchanged(); } } } } private elements gethtmlcode(string url) { try { if (!url.startswith("http://") && !url.startswith("https://")) url = "http://" + url; document doc = jsoup.connect(url).get(); elements content=doc.select("a[href]"); return content; } catch (ioexception e) { // never e.printstacktrace(), cuts off after lines , you'll // lose information that's useful debugging. use proper // logging, android's log class, check out // http://developer.android.com/tools/debugging/debugging-log.html log.e(tag, "failed load html code", e); // tell user went wrong (keep simple, // no stacktraces): toast.maketext(this, "failed load html code", toast.length_short).show(); } return null; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: // id represents home or button. in case of // activity, button shown. use navutils allow users // navigate 1 level in application structure. // more details, see navigation pattern on android design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // navutils.navigateupfromsametask(this); return true; } return super.onoptionsitemselected(item); }
there bunch of documentation on topic. can try this
private class downloadfilestask extends asynctask<url, integer, long> { protected void onpreexecute (){ dialog = progressdialog.show(youractivity.this ,"title","message"); } protected void doinbackground(url... urls) { //add function want call in async task here for(int i=0; < urls.length;i++){ elements elem= gethtmlcode(urls[i]); for(element link:elem){ if(/*link.absurl(sites[i]).contains(link.attr("abs:href")) &&*/ link.text().tolowercase().contains(keyword)){ madapter.add(link.text() + ": " + link.attr("abs:href")); madapter.notifydatasetchanged(); } } } } protected void onpostexecute(long result) { dialog.dismiss(); } }
then can call ever need with:
new downloadfilestask().execute(url1, url2, url3);
Comments
Post a Comment