Last active
January 16, 2016 13:33
-
-
Save enrichman/287c2606474239c67a0a to your computer and use it in GitHub Desktop.
Simple RSS example with PkRSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NewsActivity extends AppCompatActivity { | |
private SwipeRefreshLayout mSwipeRefreshLayout; | |
private NewsRecyclerView mListView; | |
private NewsCallback newsCallback = new NewsCallback(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_news); | |
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); | |
mSwipeRefreshLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.blue); | |
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |
@Override | |
public void onRefresh() { | |
refreshContent(); | |
} | |
}); | |
mListView = (NewsRecyclerView) findViewById(R.id.news_list_view); | |
refreshContent(); | |
} | |
private void refreshContent() { | |
mSwipeRefreshLayout.setRefreshing(true); | |
PkRSS.with(NewsActivity.this) | |
.load("http://myurl.com/news.rss") | |
.callback(newsCallback).async(); | |
} | |
private List<News> parseNews(List<Article> articleList) { | |
List<News> newsList = new ArrayList<>(); | |
for(Article article : articleList) { | |
News news = new News(); | |
String title = StringEscapeUtils.unescapeHtml4(article.getTitle()); | |
news.setTitle(title); | |
newsList.add(news); | |
} | |
return newsList; | |
} | |
private class NewsCallback implements Callback { | |
@Override | |
public void onPreload() { /* do nothing */ } | |
@Override | |
public void onLoaded(List<Article> newArticles) { | |
List<News> newsList = parseNews(newArticles); | |
mListView.setNews(newsList); | |
mSwipeRefreshLayout.setRefreshing(false); | |
} | |
@Override | |
public void onLoadFailed() { | |
mSwipeRefreshLayout.setRefreshing(false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment