AlphabetIndexer.getSectionForPosition() Always Returns 0
I am trying make the letters appear when I do fast scrolling on my
ListView. For some reason, the letter that appears is always the first
letter in my alphabet (a space). It appears that getSectionForPosition()
always returns 0, even though it should not. Below is my code:
activity_host.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HostActivity" >
<ListView android:id="@+id/song_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true" />
</RelativeLayout>
HostActivity.java
public class HostActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_host);
loadSongs();
}
private void loadSongs() {
Uri songsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = SongCursorAdapter.SONG_COLUMNS;
String songFilter = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String orderBy = SongCursorAdapter.ORDERED_BY;
Cursor songCursor = new CursorLoader(this, songsUri, projection,
songFilter, null, orderBy).loadInBackground();
SongCursorAdapter songsAdapter = new SongCursorAdapter(this,
songCursor, 0);
ListView songView = (ListView) this.findViewById(R.id.song_list);
songView.setAdapter(songsAdapter);
}
}
SongCursorAdapter.java
class SongCursorAdapter extends CursorAdapter implements SectionIndexer {
public final static String[] SONG_COLUMNS = {
MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.TITLE_KEY, MediaStore.Audio.Media.DATA };
public final static String ORDERED_BY =
MediaStore.Audio.Media.DEFAULT_SORT_ORDER;
private Context context;
private AlphabetIndexer indexer;
SongCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
this.context = context;
setupIndexer(c);
}
SongCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
this.context = context;
setupIndexer(c);
}
private void setupIndexer(Cursor c) {
int sortedByColumn = c.getColumnIndexOrThrow(ORDERED_BY);
String alphabet = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
indexer = new AlphabetIndexer(c, sortedByColumn, alphabet);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// code irrelevant
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view =
LayoutInflater.from(context).inflate(R.layout.song_layout, null);
return view;
}
@Override
public Cursor swapCursor(Cursor c) {
if (c != null) {
setupIndexer(c);
}
return super.swapCursor(c);
}
@Override
public int getPositionForSection(int section) {
return indexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
return indexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
return indexer.getSections();
}
}
EDIT: No letters are appearing at all now. The navigation bar behaves
oddly, though. Every time you scroll down, it jumps back up to the top.
No comments:
Post a Comment