5

I am modifying an adapter view. How do I implement the setSelection() of an AdapterView? What are the steps one must take when setSelection() method of an adapterview is called?

I tried browsing through ListView's source code, but it wasn't of much help.

Akram
  • 7,548
  • 8
  • 45
  • 72
pankajagarwal
  • 13,462
  • 14
  • 54
  • 65
  • Hey, i have the same problem. I try to realize the "selection" with onItemClick by saving the position of the clicked item. But the notifyDataSetChanged() did not work like i need it :( ... did you solved this problem?? – Informatic0re May 04 '12 at 08:41
  • @Mirko no I'm still having the same problem – pankajagarwal May 07 '12 at 04:58

4 Answers4

1

Save the selected position and pass it to the BaseAdapter class. (You have to implement a custom Base Adapter class).

Then in getView() method, change as per your requirement by checking position==selectedPosition. (Note: you should call notifyDatasetChange() method to invoke getView() method again).

0

Android-HorizontalListView is having HorizontalListView, which is an Android ListView widget which scrolls in a horizontal manner. In this class it's describes how to implement the setSelection() of an AdapterView.

Sagar
  • 3,159
  • 3
  • 29
  • 28
0

try List view onItemClick()

listView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position,long id) {  

            }           
        });
ilango j
  • 5,967
  • 2
  • 28
  • 25
  • i want to implement the setSelectionMethod() the onItemClickListener works fine – pankajagarwal Jul 27 '11 at 07:13
  • hi, i can't understand really what u need. i think you are trying if you selected one item before modifying adapter and after modified adapter the selected position will be the same am i right? – ilango j Jul 29 '11 at 13:04
  • I want to implement the setSelection() method. To be more clear, after populating the adapter, when my adapter starts rendering the left-most item of the list is 0. But if I call `setSelection(4)` then the leftmost item will be the 5 item of the list – pankajagarwal Jul 30 '11 at 04:22
  • ok, create a public method in adapter class, in that reset the array list (i.e from where you have to start list i mean start from 5th item means remove upto 4th item from arraylist and call this.setNotifyDatasetChanged() in that method. – ilango j Jul 30 '11 at 08:29
  • but I don't want to remove the first 4 entries from the list. Maybe I'm not able to explain you what I need. Just try the setSelection() of any standard adapterview (like gallery, Listview, Gridview) and the what happens – pankajagarwal Jul 30 '11 at 09:23
  • @llango j -- right , if i say setSelection(100) and the list currently is at 0th position then it should scroll to 100th position where the selected item has its visibility to VISIBLE – funkmaster Jan 15 '12 at 13:40
0

According to this and this you have to save selected item somewhere, show user that this item is selected(if you are not in touch mode), and you need to scroll view to this item.

Jin35
  • 8,602
  • 3
  • 32
  • 52