Zankhna
Zankhna

Reputation: 4563

Showing Alphabet Index in android listview

I have created listview and i want to implement sectionindexer and alphabetIndexer in my listview. I am reffering http://www.anddev.org/tutalphabetic_fastscroll_listview_-_similar_to_contacts-t10123.html. That works excellent. But for my code , i am not getting how to implement that functionality.

here is my list_item.xml code :

<?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"
        />

My JAVA code :

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);

so how to implement sectionindexer with my this listadapter. Please suggest me some solution. Thanx.

Upvotes: 2

Views: 2958

Answers (1)

Artyom Kiriliyk
Artyom Kiriliyk

Reputation: 2513

You can use this library. This is the simplest Section Adapter available for Android's ListView.

Upvotes: 3

Related Questions