diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-04-07 17:23:13 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-04-07 17:23:13 -0700 |
commit | 6592eeec50d35d3b543b33a58bccf2fde680c121 (patch) | |
tree | 8e30587847eaa6084b89cff2886a12591fb841a5 /src/github/daneren2005 | |
parent | 454b59dea73407db84076da130f260d58386768e (diff) | |
download | dsub-6592eeec50d35d3b543b33a58bccf2fde680c121.tar.gz dsub-6592eeec50d35d3b543b33a58bccf2fde680c121.tar.bz2 dsub-6592eeec50d35d3b543b33a58bccf2fde680c121.zip |
Fixed #486: add letter index to alphabetical album listings
Diffstat (limited to 'src/github/daneren2005')
-rw-r--r-- | src/github/daneren2005/dsub/adapter/AlbumListAdapter.java | 74 |
1 files changed, 72 insertions, 2 deletions
diff --git a/src/github/daneren2005/dsub/adapter/AlbumListAdapter.java b/src/github/daneren2005/dsub/adapter/AlbumListAdapter.java index 2e91d949..b2fcded3 100644 --- a/src/github/daneren2005/dsub/adapter/AlbumListAdapter.java +++ b/src/github/daneren2005/dsub/adapter/AlbumListAdapter.java @@ -23,17 +23,21 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.SectionIndexer; + import com.commonsware.cwac.endless.EndlessAdapter; import github.daneren2005.dsub.R; import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.ServerInfo; import github.daneren2005.dsub.service.MusicService; import github.daneren2005.dsub.service.MusicServiceFactory; -import github.daneren2005.dsub.util.Util; +import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; -public class AlbumListAdapter extends EndlessAdapter { +public class AlbumListAdapter extends EndlessAdapter implements SectionIndexer { Context context; ArrayAdapter<MusicDirectory.Entry> adapter; String type; @@ -41,6 +45,10 @@ public class AlbumListAdapter extends EndlessAdapter { int size; int offset; List<MusicDirectory.Entry> entries; + + private boolean shouldIndex = false; + private Object[] sections; + private Integer[] positions; public AlbumListAdapter(Context context, ArrayAdapter<MusicDirectory.Entry> adapter, String type, String extra, int size) { super(adapter); @@ -50,6 +58,11 @@ public class AlbumListAdapter extends EndlessAdapter { this.extra = extra; this.size = size; this.offset = size; + + if("alphabeticalByName".equals(this.type)) { + shouldIndex = true; + recreateIndexes(); + } } @Override @@ -73,6 +86,7 @@ public class AlbumListAdapter extends EndlessAdapter { adapter.add(entry); } offset += entries.size(); + recreateIndexes(); } @Override @@ -81,4 +95,60 @@ public class AlbumListAdapter extends EndlessAdapter { progress.setVisibility(View.VISIBLE); return progress; } + + private void recreateIndexes() { + if(!shouldIndex) { + return; + } + + Set<String> sectionSet = new LinkedHashSet<String>(30); + List<Integer> positionList = new ArrayList<Integer>(30); + for (int i = 0; i < adapter.getCount(); i++) { + MusicDirectory.Entry entry = adapter.getItem(i); + String index = entry.getAlbum().substring(0, 1); + if(!Character.isLetter(index.charAt(0))) { + index = "#"; + } + + if (!sectionSet.contains(index)) { + sectionSet.add(index); + positionList.add(i); + } + } + sections = sectionSet.toArray(new Object[sectionSet.size()]); + positions = positionList.toArray(new Integer[positionList.size()]); + } + + @Override + public Object[] getSections() { + if(sections != null) { + return sections; + } else { + return new Object[0]; + } + } + + @Override + public int getPositionForSection(int section) { + if(sections != null) { + section = Math.min(section, positions.length - 1); + return positions[section]; + } else { + return 0; + } + } + + @Override + public int getSectionForPosition(int pos) { + if(sections != null) { + for (int i = 0; i < sections.length - 1; i++) { + if (pos < positions[i + 1]) { + return i; + } + } + return sections.length - 1; + } else { + return 0; + } + } } |