Skip to content

Instantly share code, notes, and snippets.

@madhu314
madhu314 / ExceptionCommunication.java
Created September 20, 2012 17:08
Asynchronous exception communication between Service and UIComponent in Android
//lets say you have service XYService and a UI component XYFragment
//this piece of code goes in XYFragment
Intent serviceIntent = new Intent(context, XYService.class);
// add result receiver
serviceIntent.putExtra(XYService.RESULT_RECEIVER, new ResultReceiver(null) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// receive the result for this call
@madhu314
madhu314 / Article.java
Last active January 14, 2017 14:21
Article.java
@AutoValue public abstract class Article implements Parcelable {
//.....
public static final ArticleTimestampComparator timestampComparator =
new ArticleTimestampComparator();
public static final ArticleAuthorComparator authorComparator =
new ArticleAuthorComparator();
public static final ArticleContentComparator contentComparator =
new ArticleContentComparator();
public static final ArticleCategoryComparator categoryComparator =
new ArticleCategoryComparator();
public static class ArticleDataset {
private static final int DAYS_PRIOR = 20;
SortedList<Article> sortedList = null;
public ArticleDataset(final RecyclerView recyclerView, final RecyclerView.Adapter adapter) {
this.sortedList = new SortedList<>(Article.class,
new SortedList.BatchedCallback<>(new SortedListAdapterCallback<Article>(adapter) {
@Override public int compare(Article a1, Article a2) {
return a1.compare(a2);
//....
private void setupAdapter(Bundle savedInstanceState) {
adapter = createNewAdapter();
dataset = createDataset(adapter);
adapter.articleDataset(dataset);
recyclerView.setAdapter(adapter);
//...
}
@Override protected ArticleDataset createDataset(ArticlesAdapter adapter) {
@Override public void onBindViewHolder(ViewHolder holder, int position) {
Article article = articleDataset.getArticle(position);
holder.bindTo(article, this);
}
@Override public int getItemCount() {
return articleDataset == null ? 0 : articleDataset.size();
}
private void handleEvent(String eventName, Object data) {
if (HorizontalArticlesFragment.ITEM_CLICKED.equals(eventName)) {
Article article = (Article) data;
getHorizontalArticlesFragment().removeArticle(article.dupe());
getArticleGridFragment().addArticle(article.dupe());
} else if (ArticleGridFragment.ITEM_CLICKED.equals(eventName)) {
Article article = (Article) data;
getArticleGridFragment().removeArticle(article.dupe());
getHorizontalArticlesFragment().addArticle(article.dupe());
}
//somewhere in activity
private void showSortOptionsDialog() {
ArrayAdapter<String> adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[] {
"Timestamp", "Category", "Author", "Content"
});
new AlertDialog.Builder(this).setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialogInterface, int position) {
ArticleSortOptionsActivity.this.newSortType = position;
}
@AutoValue public static abstract class SectionArticle {
@Nullable public abstract Article article();
@Nullable public abstract String category();
public static SectionArticle createCategory(Article article) {
return new AutoValue_SectionArticlesActivity_SectionArticle(null, article.category());
}
public static SectionArticle createArticle(Article article) {
public void add(Article article) {
sortedList.beginBatchedUpdates();
sortedList.add(SectionArticle.createCategory(article));
sortedList.add(SectionArticle.createArticle(article));
sortedList.endBatchedUpdates();
}
public void remove(Article article) {
SectionArticle sectionArticle = SectionArticle.createArticle(article);
sortedList.beginBatchedUpdates();
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="0dp"