I want to change the position of an item in ListView dynamically.How can I do that?
Asked
Active
Viewed 1.2k times
1
-
What do you mean with "change position"? swap item on place 2 with item on place 3 ? – Heiko Rupp Apr 29 '11 at 06:33
-
yes,I want to have a button in my list view so on pressing that button that row should shift to first position in listview. – Arun Apr 29 '11 at 06:41
-
Yes,I want to have a button on list row so on pressing that button the row should shift to first position. – Arun Apr 29 '11 at 06:45
3 Answers
5
The ListView is backed up by some data structure (e.g. a List<String>
). So you can do
Pseudocode:
List<String> list = ...
ListView lv = ..
Adapter a = new Adapter(..., list);
lv.setOnClickListener(this);
onItemPressed(..., int position, ...) {
tmp = list.get(0);
list.set(0, list.get(position));
list.set(position,tmp);
a.notifyDataSetChanged();
}

Heiko Rupp
- 30,426
- 13
- 82
- 119
3
Check the following link
This is working fine for me
http://ericharlow.blogspot.in/2010/10/experience-android-drag-and-drop-list.html
https://sites.google.com/site/ericbharlow/sample-code/DragNDrop.zip?attredirects=0&d=1

Dipali
- 1,148
- 3
- 13
- 23
0
do this for change position of listview in coding
ListView.setSelection(position);

Niranj Patel
- 32,980
- 10
- 97
- 133