1

I want to change the position of an item in ListView dynamically.How can I do that?

Arun
  • 11
  • 1
  • 1
  • 2
  • 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 Answers3

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
0

do this for change position of listview in coding

ListView.setSelection(position);
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133