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