我已经创建了列表视图,我想在我的列表视图中实现 sectionindexer 和 alphabetIndexer。我正在引用http://www.anddev.org/tutalphabetic_fastscroll_listview_-_similar_to_contacts-t10123.html。效果很好。但是对于我的代码,我不知道如何实现该功能。
这是我的 list_item.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/list_selector"
android:padding="5dp"
android:id="@+id/tab">
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/musicsymbol"/>
</LinearLayout>
<TextView
android:id="@+id/songTitle"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_alignTop="@+id/thumbnail"
android:color="#10bcc9"
android:maxLines="1"
android:layout_toRightOf="@+id/thumbnail"
/>
<TextView
android:id="@+id/duration"
android:layout_width="56dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/songTitle"
android:gravity="right"
android:layout_marginRight="5dip"
android:textSize="15dip"
android:textColor="#10bcc9"
android:textStyle="bold"
android:maxLines="1"
/>
<TextView
android:id="@+id/songArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/songTitle"
android:textSize="14dp"
android:color="#10bcc9"
android:textStyle="bold"
android:lines="1"
android:layout_toRightOf="@+id/thumbnail"
/>
我的 JAVA 代码:
Cursor cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null , null, null,MediaStore.Audio.Media.TITLE );
if (cursor == null)
{
//Query Failed , Handle error.
}
else if (!cursor.moveToFirst())
{
//No media on the device.
}
else
{
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndexOrThrow(android.provider.MediaStore.Audio.Media.DATA);
int artistcolumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int durationcolumn =cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
for(int i=0;i<cursor.getCount();i++)
{
String thisTitle = cursor.getString(titleColumn);
String path = cursor.getString(idColumn);
String artist = cursor.getString(artistcolumn);
Long duration = cursor.getLong(durationcolumn);
Utilities objUtilities = new Utilities();
String timeDuration = objUtilities.milliSecondsToTimer(duration);
HashMap<String, String> song = new HashMap<String, String>();
song.put("songTitle",thisTitle);
song.put("songPath", path);
song.put("artist", artist);
song.put("duration",timeDuration);
// Adding each song to SongList
songsList.add(song);
cursor.moveToNext();
}
}
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
String[] from = {"songTitle", "artist" , "duration"};
int[] to={R.id.songTitle,R.id.songArtist, R.id.duration};
adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, from, to);
setListAdapter(adapter);
那么如何用我的这个listadapter实现sectionindexer。请建议我一些解决方案。谢谢。