3

I have a list view of 300 mp3 songs and suppose song 52 is playing. So I want next time when user opens the list of song, the list row in front of him must starts from row 52 (while up and down are still rows). I tried using

public View getViewByPosition(int pos, ListView listView) {
    final int firstListItemPosition = listView.getFirstVisiblePosition();
    final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1;

    if (pos < firstListItemPosition || pos > lastListItemPosition ) {
        return listView.getAdapter().getView(pos, null, listView);
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}

and also

adapter.getItem(52);

But both are pointless methods and dont take me to row 52. (These methods are what I used, but might not be the solution) Also I got no solution googling it. Please let me know if you guys can deal with this !!

A big thanks to you for listening!

2 Answers2

11

This line will select n row starting from top:

listview.setSelectionFromTop(n, 0);

So for your case as you want to select 52 row you have to right following code on start of activity after setting your adapter

listview.setSelectionFromTop(52, 0);
Anjali
  • 1
  • 1
  • 13
  • 20
1

try this: link

listview.smoothScrollToPosition(yourPosition);
balaji koduri
  • 1,321
  • 9
  • 25