Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:58:21+00:00 2026-06-14T23:58:21+00:00

Here I am Creating ListView which shows songlist. And on list item click i

  • 0

Here I am Creating ListView which shows songlist. And on list item click i am passing the position of the song to play it. Means i have functionlke playSong(Int Index). That works perfactly. But when i perform search ,it shows searchable item and on that item click it i am not gettig the real position of it.

example :

list shows me items like 1) Be with You 2) Can you here me 3) let it be

but now when i search “can you here me” , it shows list with “can you here me”. And when i click on this item , It takes 0 index and plays “Be with You” song.

so what is missing in my code ?? Please suggest me.

Here is my code :

ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
        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);

        // selecting single ListView item
        ListView lv = getListView();

        // listening to single listitem click
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int songIndex = position;

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        AndroidBuildingMusicPlayerActivity.class);

                // Sending songIndex to PlayerActivity
                in.putExtra("songIndex", songIndex);                
                startActivity(in);  

            }
        });

        inputSearch = (EditText) findViewById(R.id.inputSearch);
        inputSearch.addTextChangedListener(new TextWatcher() {


            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
            ((SimpleAdapter)getListAdapter()).getFilter().filter(cs);   

            }


            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }


            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                          
            }
        });

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:58:23+00:00Added an answer on June 14, 2026 at 11:58 pm

    What you can do is override the getItemId method of your adapter to return the ‘real’ song index. Then you will retrieve this index from the id parameter of your onItemClick callback.

    Try this:

        adapter = new SimpleAdapter(this, songsListData, R.layout.playlist_item, from, to) {
            public long getItemId(int position) { 
                return songsListData.indexOf(getItem(position));
            }
        };
    

    And make sure that your songsListData is declared final (so that you can access it from inside the adapter):

    final ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
    

    Then in your onItemClick method just use id instead of position:

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting listitem index
                int songIndex = id;
                ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my Sencha Touch 2 code which shows the JSON list data in
I have a custom ListView Adapter , with which I'm creating rows for a
I have a code for dictionary application. If user click list item in the
I am creating a ListView which as a TextView and ImageView as a list
Here i have created expandable list view which is having group row for Artist
I am developing an application in android in which i creating a list view
In my form I have a listview and a panel which is used as
I currently have a custom listview with two rows of text in each item,
Am following the tutorial here on creating a simple todo list app: http://www.mydroid.apnafundaz.com/2010/07/todolist-application-in-android-step-by-step-guide-with-screen-shots/ Have
I need some help creating a listview inside a layout i have. im having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.