Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Merge pull request #64 from carlbenson/master
Browse files Browse the repository at this point in the history
Allow headers to come from other row types than strings.
  • Loading branch information
thorbenprimke authored Jul 8, 2016
2 parents 9687e3b + e811c40 commit dd2e594
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public RowWrapper(boolean isRealm, int realmIndex, int sectionHeaderIndex, Strin
private final int LOAD_MORE_VIEW_TYPE = 101;
private final int FOOTER_VIEW_TYPE = 102;

private Context context;
protected LayoutInflater inflater;
protected RealmResults<T> realmResults;
protected List ids;
Expand Down Expand Up @@ -151,6 +152,7 @@ public RealmBasedRecyclerViewAdapter(
throw new IllegalArgumentException("Context cannot be null");
}

this.context = context;
this.animateResults = animateResults;
this.addSectionHeaders = addSectionHeaders;
this.headerColumnName = headerColumnName;
Expand Down Expand Up @@ -231,6 +233,10 @@ public void onBindHeaderViewHolder(RealmViewHolder holder, int position) {
}
}

public Context getContext() {
return context;
}

/**
* DON'T OVERRIDE THIS METHOD. Implement onCreateRealmViewHolder instead.
*/
Expand Down Expand Up @@ -354,8 +360,20 @@ public void updateRealmResults(RealmResults<T> queryResults) {
* Method that creates the header string that should be used. Override this method to have
* a custom header.
*/
public String createHeaderFromColumnValue(String columnValue) {
return columnValue.substring(0, 1);
public String createHeaderFromColumnValue(Object columnValue) {
String result = null;
if (columnValue instanceof Boolean) {
result = columnValue.toString();
} else if (columnValue instanceof String) {
result = ((String) columnValue).substring(0, 1);
} else if (columnValue instanceof Long) {
result = columnValue.toString();
} else {
throw new IllegalStateException("columnType not supported");
}

return result;

}

private List getIdsOfRealmResults() {
Expand Down Expand Up @@ -426,8 +444,24 @@ private void updateRowWrappers() {
final long headerIndex = realmResults.getTable().getColumnIndex(headerColumnName);
int i = 0;
for (RealmModel result : realmResults) {
String rawHeader = ((RealmObjectProxy) result)
.realmGet$proxyState().getRow$realm().getString(headerIndex);
Object rawHeader;
RealmFieldType fieldType = ((RealmObjectProxy) result)
.realmGet$proxyState().getRow$realm().getColumnType(headerIndex);

if (fieldType == RealmFieldType.STRING) {
rawHeader = ((RealmObjectProxy) result)
.realmGet$proxyState().getRow$realm().getString(headerIndex);

} else if (fieldType == RealmFieldType.BOOLEAN) {
rawHeader = ((RealmObjectProxy) result)
.realmGet$proxyState().getRow$realm().getBoolean(headerIndex);
} else if (fieldType == RealmFieldType.INTEGER) {
rawHeader = ((RealmObjectProxy) result)
.realmGet$proxyState().getRow$realm().getLong(headerIndex);
} else {
throw new IllegalStateException("columnValue type not supported");
}

String header = createHeaderFromColumnValue(rawHeader);
if (!TextUtils.equals(lastHeader, header)) {
// Insert new header view and update section data.
Expand Down

0 comments on commit dd2e594

Please sign in to comment.