1

I'm writing application with ListView which contains names and ratings. After click on stars (RatingBar) elements are sorted according to ranking. ListView is reorganized, but I'd like to see the item which was clicked, it means ListView should automatically scroll to that item. Could somebody give me some tips? I have no idea what I could use.

  • What is listView to set in the getView method? I think that might be the problem. Try getListView() instead. – ElectronicGeek Apr 30 '14 at 20:49
  • listView is my ListView of applications. With getListView() I get that is undefined for the type new RatingBar.OnRatingBarChangeListener() in my adapter. By the way, I have problem with correct sorting according to the ranking (it doesn't sort all elements at once) – user3590445 May 01 '14 at 09:54

1 Answers1

2

See this article for info on how to scroll to an item: Programmatically scroll to a specific position in an Android ListView

You'd use getListView().smoothScrollToPosition(position) where position is the index you want to scroll to.

Community
  • 1
  • 1
ElectronicGeek
  • 3,312
  • 2
  • 23
  • 35
  • Could you advise me where should I paste it? I sort adapter in onRatingChange and there, after sorting, I set listView.smoothScrollToPosition(adapter.getPosition(object)); where 'object' is element that I clicked, but it doesn't work correctly – user3590445 Apr 30 '14 at 19:42
  • It must not be finding the object, are you sure the object is in the adapter which is set to the ListView? – ElectronicGeek Apr 30 '14 at 19:43
  • I do: sort, adapter.notifyDataSetChanged and smoothScroll. So, I guess, that I do smoothScroll on listView which has updated adapter. I'm sorry, I'm beginner and sometimes it hard for me to imagine. – user3590445 Apr 30 '14 at 20:04
  • I don't know where the problem possibly could be. The only thing I could think of is that if the ListView is too small, it can't scroll, or the ListView reference doesn't point to the on-screen one. How many items does the ListView have? – ElectronicGeek Apr 30 '14 at 20:06
  • It works from time to time.. It's list of intalled applications. – user3590445 Apr 30 '14 at 20:08
  • Are there any patterns in how it works? I think the problem is that you're not getting a valid int from the adapter.getPosition(object). – ElectronicGeek Apr 30 '14 at 20:09
  • Maybe problem is that I do it in onRatingChanged in getView in adapter? – user3590445 Apr 30 '14 at 20:09
  • If you do it in the getView method, then the ListView probably won't be initialized. – ElectronicGeek Apr 30 '14 at 20:10
  • So, where should I do it? – user3590445 Apr 30 '14 at 20:12
  • I would recommend to do it in a method that gets called after the getView. Maybe the one that's called to sort the list, I'm not sure. – ElectronicGeek Apr 30 '14 at 20:15
  • Can you post the code for the Adapter/ListView? I think that would make it easier. – ElectronicGeek Apr 30 '14 at 20:26
  • Can you paste it in your question, or on pastebin, maybe? – ElectronicGeek Apr 30 '14 at 20:38
  • I think it would be best to create a new question for this problem. It's just cluttering up the post, and I believe I answered your base question. If you add all your code and necessary items to the other post, it'd be easier to solve. That way new people could see the question in better detail. – ElectronicGeek May 01 '14 at 12:43