How to Merge two or more adapters and show in one ListView in Android -
i have listview
custom adapters returning both twitter , facebook feeds. have own xml. till showing them in separate activity
, planning combine both data , show in 1 adapter. heard there concept "merge adapters". can me below posted code ?
file : facebookadapter.java
:
public class facebookadapter extends arrayadapter<rssfeedstructure> { list<rssfeedstructure> imageandtexts1 = null; public facebookadapter(activity activity, list<rssfeedstructure> imageandtexts) { super(activity, 0, imageandtexts); imageandtexts1 = imageandtexts; } @override public view getview(int position, view convertview, viewgroup parent) { activity activity = (activity) getcontext(); layoutinflater inflater = activity.getlayoutinflater(); view rowview = inflater.inflate(r.layout.facebookadapter, null); textview textview = (textview) rowview.findviewbyid(r.id.feed_text); textview timefeedtext = (textview) rowview .findviewbyid(r.id.feed_updatetime); imageview imageview = (imageview) rowview.findviewbyid(r.id.feed_image); try { log.d("rssfeed", "imageandtexts1.get(position).getimglink() :: " + imageandtexts1.get(position).getimglink() + " :: " + imageandtexts1.get(position).gettitle()); textview.settext(imageandtexts1.get(position).getdescription()); spannablestring content = new spannablestring(imageandtexts1.get( position).getpubdate()); content.setspan(new underlinespan(), 0, 13, 0); timefeedtext.settext(content); if (imageandtexts1.get(position).getimglink() != null) { url feedimage = new url(imageandtexts1.get(position) .getimglink().tostring()); if (!feedimage.tostring().equalsignorecase("null")) { httpurlconnection conn = (httpurlconnection) feedimage .openconnection(); inputstream = conn.getinputstream(); bitmap img = bitmapfactory.decodestream(is); imageview.setimagebitmap(img); } else { imageview.setbackgroundresource(r.drawable.rss_tab_tweets); } } } catch (malformedurlexception e) { } catch (ioexception e) { } return rowview; } }
file : facebookactivity.java
:
public class facebookfeeds extends activity { /** called when activity first created. */ listview _rssfeedlistview; list<jsonobject> jobs; list<rssfeedstructure> rssstr; private facebookadapter _adapter; string sorti = ""; string mode = ""; //button sort_btn; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview); _rssfeedlistview = (listview) findviewbyid(r.id.rssfeed_listview); rssfeedtask rsstask = new rssfeedtask(); rsstask.execute(); } private class rssfeedtask extends asynctask<string, void, string> { // private string content; private progressdialog dialog; string response = ""; @override protected void onpreexecute() { dialog = new progressdialog(facebookfeeds.this); dialog.setmessage("rss loading..."); dialog.show(); } @override protected string doinbackground(string... urls) { try { string feed = "https://someurl"; xmlhandler rh = new xmlhandler(); rssstr = rh.getlatestarticles(feed); } catch (exception e) { } return response; } @override protected void onpostexecute(string result) { if (rssstr != null) { _adapter = new facebookadapter(facebookfeeds.this, rssstr); _rssfeedlistview.setadapter(_adapter); } dialog.dismiss(); } } }
file : twitteradapter.java
:
public class twitteradapter extends arrayadapter<rssfeedstructure> { list<rssfeedstructure> imageandtexts1 = null; public twitteradapter(activity activity, list<rssfeedstructure> imageandtexts) { super(activity, 0, imageandtexts); imageandtexts1 = imageandtexts; } @override public view getview(int position, view convertview, viewgroup parent) { activity activity = (activity) getcontext(); layoutinflater inflater = activity.getlayoutinflater(); view rowview = inflater.inflate(r.layout.twitteradapter, null); textview textview = (textview) rowview.findviewbyid(r.id.feed_text); textview timefeedtext = (textview) rowview .findviewbyid(r.id.feed_updatetime); imageview imageview = (imageview) rowview.findviewbyid(r.id.feed_image); try { log.d("rssfeed", "imageandtexts1.get(position).getimglink() :: " + imageandtexts1.get(position).getimglink() + " :: " + imageandtexts1.get(position).gettitle()); textview.settext(imageandtexts1.get(position).gettitle()); spannablestring content = new spannablestring(imageandtexts1.get( position).getpubdate()); content.setspan(new underlinespan(), 0, 13, 0); timefeedtext.settext(content); if (imageandtexts1.get(position).getimglink() != null) { url feedimage = new url(imageandtexts1.get(position) .getimglink().tostring()); if (!feedimage.tostring().equalsignorecase("null")) { httpurlconnection conn = (httpurlconnection) feedimage .openconnection(); inputstream = conn.getinputstream(); bitmap img = bitmapfactory.decodestream(is); imageview.setimagebitmap(img); } else { imageview.setbackgroundresource(r.drawable.rss_tab_tweets); } } } catch (malformedurlexception e) { } catch (ioexception e) { } return rowview; } }
file : twitteractivity.java
:
public class twitterfeeds extends activity { /** called when activity first created. */ listview _rssfeedlistview; list<jsonobject> jobs; list<rssfeedstructure> rssstr; private twitteradapter _adapter; string sorti = ""; string mode = ""; //button sort_btn; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview); _rssfeedlistview = (listview) findviewbyid(r.id.rssfeed_listview); rssfeedtask rsstask = new rssfeedtask(); rsstask.execute(); } private class rssfeedtask extends asynctask<string, void, string> { // private string content; private progressdialog dialog; string response = ""; @override protected void onpreexecute() { dialog = new progressdialog(twitterfeeds.this); dialog.setmessage("rss loading..."); dialog.show(); } @override protected string doinbackground(string... urls) { try { string feed = "https://someurl"; xmlhandler rh = new xmlhandler(); rssstr = rh.getlatestarticles(feed); } catch (exception e) { } return response; } @override protected void onpostexecute(string result) { if (rssstr != null) { _adapter = new twitteradapter(twitterfeeds.this, rssstr); _rssfeedlistview.setadapter(_adapter); } dialog.dismiss(); } } }
maybe mergeadapter can : https://github.com/commonsguy/cwac-merge
Comments
Post a Comment