java - How to fetch rss feeds using xml parser in a navigation drawer activity -
i new android please pardon language. followed tutorial this link develop rss feed app. xml parser used recycler view fetch , display rss feed in activity called articles. feed includes pubdate, link, title, description , thumb_nail(with of picasso library). app works fine. now, want make articles fragment in navigation drawer. here code:
public class articles extends appcompatactivity implements view.onclicklistener{
recyclerview recyclerview; private boolean isfabopen = false; private floatingactionbutton fab,fab1,fab2,fab3,fab4,fab5,fab6,fab7; private animation fab_open,fab_close,rotate_forward,rotate_backward; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.articles_activity); if(!isnetworkconnected()){ alertdialog.builder builder = new alertdialog.builder(this); builder.setcancelable(false); builder.settitle("oops!"); builder.seticon(r.drawable.ic_error_wine_24dp); builder.setmessage("no internet connection."); builder.setpositivebutton("retry", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { //if user select "no", cancel dialog , continue app /* finish(); */ // try below // intent restartintent = new intent(home.this, home.class); // restartintent.setdata(getintent().getdata()); intent restartintent = getintent(); restartintent.setdata(getintent().getdata()); finish(); startactivity(restartintent); } }); builder.setnegativebutton("exit", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { //if user select "no", cancel dialog , continue app dialog.cancel(); finish(); } }); alertdialog alert = builder.create(); alert.show(); } else { if (isnetworkconnected()) { // startactivity(new intent(home.this, home.class)); recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); articlesrss readrss = new articlesrss (this, recyclerview); readrss.execute(); } // return networkinfo != null; // } } toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); fab = (floatingactionbutton)findviewbyid(r.id.fab); fab1 = (floatingactionbutton)findviewbyid(r.id.fab1); fab2 = (floatingactionbutton)findviewbyid(r.id.fab2); fab3 = (floatingactionbutton)findviewbyid(r.id.fab3); fab4 = (floatingactionbutton)findviewbyid(r.id.fab4); fab5 = (floatingactionbutton)findviewbyid(r.id.fab5); fab6 = (floatingactionbutton)findviewbyid(r.id.fab6); fab7 = (floatingactionbutton)findviewbyid(r.id.fab7); fab_open = animationutils.loadanimation(getapplicationcontext(), r.anim.fab_open); fab_close = animationutils.loadanimation(getapplicationcontext(),r.anim.fab_close); rotate_forward = animationutils.loadanimation(getapplicationcontext(),r.anim.rotate_forward); rotate_backward = animationutils.loadanimation(getapplicationcontext(),r.anim.rotate_backward); fab.setonclicklistener(this); fab1.setonclicklistener(this); fab2.setonclicklistener(this); // fab3.setonclicklistener(this); fab4.setonclicklistener(this); fab5.setonclicklistener(this); fab6.setonclicklistener(this); fab7.setonclicklistener(this); } public boolean isnetworkconnected() { connectivitymanager cm = (connectivitymanager) this.getsystemservice(context.connectivity_service); networkinfo networkinfo = cm.getactivenetworkinfo(); return networkinfo != null; } @override public void onclick(view v) { int id = v.getid(); switch (id){ case r.id.fab: animatefab(); break; case r.id.fab1: intent intent1 = new intent (articles.this, sports.class); startactivity(intent1); // log.d("raj", "fab 1"); break; case r.id.fab2: intent intent2 = new intent (articles.this, productsandservices.class); startactivity(intent2); // log.d("raj", "fab 2"); break; case r.id.fab3: /* intent intent3 = new intent (articles.this, home.class); startactivity(intent3);*/ // log.d("raj", "fab 1"); break; case r.id.fab4: intent intent4 = new intent (articles.this, events.class); startactivity(intent4); // log.d("raj", "fab 2"); break; case r.id.fab5: intent intent5 = new intent (articles.this, rumours.class); startactivity(intent5); // log.d("raj", "fab 1"); break; case r.id.fab6: intent intent6 = new intent (articles.this, entertainment.class); startactivity(intent6); // log.d("raj", "fab 2"); break; case r.id.fab7: intent intent7 = new intent (articles.this, news.class); startactivity(intent7); // log.d("raj", "fab 1"); break; } } public void animatefab(){ if(isfabopen){ fab.startanimation(rotate_backward); fab1.startanimation(fab_close); fab2.startanimation(fab_close); fab3.startanimation(fab_close); fab4.startanimation(fab_close); fab5.startanimation(fab_close); fab6.startanimation(fab_close); fab7.startanimation(fab_close); fab1.setclickable(false); fab2.setclickable(false); fab3.setclickable(false); fab4.setclickable(false); fab5.setclickable(false); fab6.setclickable(false); fab7.setclickable(false);; isfabopen = false; // log.d("raj", "close"); } else { fab.startanimation(rotate_forward); fab1.startanimation(fab_open); fab2.startanimation(fab_open); fab3.startanimation(fab_open); fab4.startanimation(fab_open); fab5.startanimation(fab_open); fab6.startanimation(fab_open); fab7.startanimation(fab_open); fab1.setclickable(true); fab2.setclickable(true); fab3.setclickable(false); fab4.setclickable(true); fab5.setclickable(true); fab6.setclickable(true); fab7.setclickable(true); isfabopen = true; // log.d("raj","open"); } } @override protected void onstart() { super.onstart(); } /** called when activity has become visible. */ @override protected void onresume() { super.onresume(); } /** called when activity taking focus. */ @override protected void onpause() { super.onpause(); } /** called when activity no longer visible. */ @override protected void onstop() { super.onstop(); } /** called before activity destroyed. */ @override public void ondestroy() { super.ondestroy(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_articles, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_options) { intent intent = new intent (articles.this, settingsactivity.class); startactivity(intent); return true; } else if (id == r.id.about) { intent intent = new intent (articles.this, aboutapp.class); startactivity(intent); return true; } else if (id == r.id.rate) { uri uri = uri.parse("market://details?id=" + getbasecontext().getpackagename()); intent gotomarket = new intent(intent.action_view, uri); // count play market backstack, after pressing button, // taken our application, need add following flags intent. gotomarket.addflags(intent.flag_activity_no_history | intent.flag_activity_new_document | intent.flag_activity_multiple_task | intent.flag_activity_no_history | intent.flag_activity_new_document | intent.flag_activity_multiple_task); try { startactivity(gotomarket); } catch (activitynotfoundexception e) { startactivity(new intent(intent.action_view, uri.parse("http://play.google.com/store/apps/details?id=" + getbasecontext().getpackagename()))); return true; } } else if (id == r.id.help) { intent intent = new intent (articles.this, feedback.class); startactivity(intent); return true; } return super.onoptionsitemselected(item); }}
public class articlesrss extends asynctask {
context context; string address = "https://urbangospeltunze.wordpress.com/feed/"; progressdialog progressdialog; arraylist<articlesfeeditem> articlesfeeditems; recyclerview recyclerview; url url; public articlesrss(context context,recyclerview recyclerview) { this.recyclerview=recyclerview; this.context = context; progressdialog = new progressdialog(context); progressdialog.setmessage("connecting..."); progressdialog.setcancelable(false); progressdialog.setcanceledontouchoutside(false); } @override protected void onpreexecute() { progressdialog.show(); super.onpreexecute(); } @override protected void oncancelled() { super.oncancelled(); } @override protected void onpostexecute(void avoid) { super.onpostexecute(avoid); progressdialog.dismiss(); articlesadapter adapter=new articlesadapter(context, articlesfeeditems); recyclerview.setlayoutmanager(new linearlayoutmanager(context)); recyclerview.additemdecoration(new verticalspace(3)); recyclerview.setadapter(adapter); //try toast toast.maketext(context, "connected!", toast.length_short).show(); } @override protected void doinbackground(void... params) { processxml(getdata()); return null; } private void processxml(document data) { if (data != null) { articlesfeeditems =new arraylist<>(); element root = data.getdocumentelement(); node channel = root.getchildnodes().item(1); nodelist items = channel.getchildnodes(); (int = 0; < items.getlength(); i++) { node cureentchild = items.item(i); if (cureentchild.getnodename().equalsignorecase("item")) { articlesfeeditem item=new articlesfeeditem(); nodelist itemchilds = cureentchild.getchildnodes(); (int j = 0; j < itemchilds.getlength(); j++) { node cureent = itemchilds.item(j); if (cureent.getnodename().equalsignorecase("title")){ item.settitle(cureent.gettextcontent()); }else if (cureent.getnodename().equalsignorecase("description")){ item.setdescription(cureent.gettextcontent()); }else if (cureent.getnodename().equalsignorecase("pubdate")){ item.setpubdate(cureent.gettextcontent()); }else if (cureent.getnodename().equalsignorecase("link")){ item.setlink(cureent.gettextcontent()); }else if (cureent.getnodename().equalsignorecase("media:content")){ //this return thumbnail url string url=cureent.getattributes().item(0).gettextcontent(); item.setthumbnailurl(url); } } articlesfeeditems.add(item); } } } } public document getdata() { try { url = new url(address); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); inputstream inputstream = connection.getinputstream(); documentbuilderfactory builderfactory = documentbuilderfactory.newinstance(); documentbuilder builder = builderfactory.newdocumentbuilder(); document xmldoc = builder.parse(inputstream); return xmldoc; } catch (exception e) { e.printstacktrace(); return null; } }}
public class articlesfeeditem {
string title; string link; string description; string pubdate; string thumbnailurl; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getlink() { return link; } public void setlink(string link) { this.link = link; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string getpubdate() { return pubdate; } public void setpubdate(string pubdate) { this.pubdate = pubdate; } public string getthumbnailurl() { return thumbnailurl; } public void setthumbnailurl(string thumbnailurl) { this.thumbnailurl = thumbnailurl; } }
public class articleswebview extends appcompatactivity {
webview mwebview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.articles_webview); mwebview = (webview) findviewbyid(r.id.articles_webview); bundle bundle = getintent().getextras(); mwebview.loadurl(bundle.getstring("link")); mwebview.getsettings().setjavascriptenabled(true); mwebview.getsettings().setsupportmultiplewindows(true); mwebview.setwebviewclient(new webclient(){ @override public void onpagefinished(webview view, string url) { //hide loading image findviewbyid(r.id.progressbar1).setvisibility(view.gone); //show webview findviewbyid(r.id.articles_webview).setvisibility(view.visible); }}); } public class webclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { view.loadurl(url); return true; }} @override public void onbackpressed() { if(mwebview.cangoback()) { mwebview.goback(); } else { super.onbackpressed(); } } @override protected void onstart() { super.onstart(); } /** called when activity has become visible. */ @override protected void onresume() { super.onresume(); } /** called when activity taking focus. */ @override protected void onpause() { super.onpause(); } /** called when activity no longer visible. */ @override protected void onstop() { super.onstop(); } /** called before activity destroyed. */ @override public void ondestroy() { super.ondestroy(); }}
public class articlesadapter extents recyclerview.adapter {
arraylist<articlesfeeditem> articlesfeeditems; context context; public articlesadapter(context context,arraylist<articlesfeeditem> articlesfeeditems){ this.articlesfeeditems = articlesfeeditems; this.context=context; } @override public myviewholder oncreateviewholder(viewgroup parent, int viewtype) { view view= layoutinflater.from(context).inflate(r.layout.articles_card,parent,false); myviewholder holder=new myviewholder(view); return holder; } @override public void onbindviewholder(myviewholder holder, int position) { // yoyo.with(techniques.fadein).playon(holder.cardview); final articlesfeeditem current= articlesfeeditems.get(position); holder.title.settext(current.gettitle()); holder.description.settext(current.getdescription()); holder.date.settext(current.getpubdate()); picasso.with(context).load(current.getthumbnailurl()).into(holder.thumbnail); holder.cardview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(context, articleswebview.class); intent.putextra("link", current.getlink()); context.startactivity(intent); } }); } @override public int getitemcount() { return articlesfeeditems.size(); } public class myviewholder extends recyclerview.viewholder { textview title,description,date; imageview thumbnail; cardview cardview; public myviewholder(view itemview) { super(itemview); title= (textview) itemview.findviewbyid(r.id.title_text); description= (textview) itemview.findviewbyid(r.id.description_text); date= (textview) itemview.findviewbyid(r.id.date_text); thumbnail= (imageview) itemview.findviewbyid(r.id.thumb_img); cardview= (cardview) itemview.findviewbyid(r.id.articles_cardview); } } }
Comments
Post a Comment