android - How is it possible to have feeds from different Urls in the same ListView -


with current project can take datas single url , display them in custom listview. there way can datas more 1 url , put in same listview if datas each url called differently? code allows me have items url:

    public  class class1 extends fragment {      private rssfeed myrssfeed = null;      public class1()     {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view view = inflater.inflate(r.layout.tab1, null);          if (android.os.build.version.sdk_int > 9) {             strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build();             strictmode.setthreadpolicy(policy);         }          try {             url rssurl = new url("url");             saxparserfactory mysaxparserfactory = saxparserfactory.newinstance();             saxparser mysaxparser = mysaxparserfactory.newsaxparser();             xmlreader myxmlreader = mysaxparser.getxmlreader();             rsshandler myrsshandler = new rsshandler();             myxmlreader.setcontenthandler(myrsshandler);             inputsource myinputsource = new inputsource(rssurl.openstream());             myxmlreader.parse(myinputsource);              myrssfeed = myrsshandler.getfeed();          } catch (malformedurlexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (parserconfigurationexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (saxexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }           if (myrssfeed!=null)         {             listview list = (listview)view.findviewbyid(android.r.id.list);             customlist adapter = new customlist(getactivity(),myrssfeed.getlist());             adapter.addall();             list.setadapter(adapter);          }         else             toast.maketext(getactivity(), "spiacente, connessione non disponibile!" +                             " prova piĆ¹ tardi.",                     toast.length_long).show();         return view;     } } 

i doing in app,

this how doing this, have created asynctask parse rss, , using loop pass different urls parsed aysnctask.

here mean,

this array of url,

private string[] newsurls = {             "url1", "url2", "url3" }; 

this how executing them,

//getnews asynctask class (int = 0; < newsurls.length; i++) {                 getnews blog = new getnews(i);                 blog.execute();             } 

in above code iis url number passed in constructor in asynctask class.

here constructor in asynctask class,

int number; public getnews(int urlnumber) {             number = urlnumber;         } 

and loading url in asynctask's doinbackground() method way,

                url feedurl = new url(newsurls[number]);                 httpurlconnection connection;                 connection = (httpurlconnection) feedurl.openconnection();                 connection.setconnecttimeout(8000);                 connection.connect(); 

after that, using saxparser parse xml based on need.

please note if want parallel execution, replace blog.execute();

blog.executeonexecutor(asynctask.thread_pool_executor);  // type of executor uses following params:             //             // private static final int core_pool_size = 5;             // private static final int maximum_pool_size = 128;             // private static final int keep_alive = 1;             //             // private static final threadfactory sthreadfactory = new threadfactory() {             //     private final atomicinteger mcount = new atomicinteger(1);             //             //     public thread newthread(runnable r) {             //         return new thread(r, "asynctask #" + mcount.getandincrement());             //     }             // };             //             // private static final blockingqueue<runnable> spoolworkqueue =             //        new linkedblockingqueue<runnable>(10); 

also note that,

when first introduced, asynctasks executed serially on single background thread. starting
donut, changed pool of threads allowing multiple tasks operate in parallel. after honeycomb, planned change single thread avoid common application errors caused parallel execution. if want parallel execution, can use executeonexecutor(executor, params...) version of method thread_pool_executor; however, see commentary there warnings on use.

donut android 1.6, honeycomb android 3.0.

you may execute task based on version have,

if (build.version.sdk_int >= build.version_codes.honeycomb) {     blog.executeonexecutor(asynctask.thread_pool_executor); } else {     blog.execute(); } 

this partial code show doing,

    // main class  private string[] newsurls = {             "url1", "url2", "url3" };     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_myapp);               (int = 0; < newsurls.length; i++) {                 getnews blog = new getnews(i);                 blog.execute();             }         }     } 

this asynctask class (partial code),

private class getnews extends         asynctask<object, void, void> {      protected int number;      public getnews(int urlnumber) {         number = urlnumber;     }      @override     protected void doinbackground(             object... arg0) {          try {             // check if feed live             url feedurl = new url(newsurls[number]);             httpurlconnection connection;             connection = (httpurlconnection) feedurl.openconnection();             connection.setconnecttimeout(8000);             connection.connect(); 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -