How to update ListView dynamically on each iteration of a sorting process in android? -
i have arraylist
of 9 integer values. want sort arraylist
based on ascending order of values. after each iteration, want display list in listview
. after next iteration, listview
should update result of new iteration. used below code sorting. dont know how refresh listview
on each iteration.
integer[] numbers = new integer[] {9,8,7,6,5,4,3,2,1}; arraylist<integer> numberlist = new arraylist<integer>(); numberlist.addall( arrays.aslist(numbers) ); list list = numberlist; //method sorting list dosort(numberlist); // create arrayadapter using number list. listadapter = new arrayadapter<integer>(this, r.layout.listdata, numberlist); // set arrayadapter listview's adapter. mainlistview.setadapter( listadapter ); } public static void swap(list<integer> sort, int i, int j) { int tmp = sort.get(i); sort.set(i, sort.get(j)); sort.set(j, tmp); } public static void dosort(list<integer> sort) { int min; (int = 0; < sort.size(); ++i) { //find minimum in rest of array min = i; (int j = + 1; j < sort.size(); ++j) { if (sort.get(j) < sort.get(min)) { min = j; } } //do swap swap(sort, i, min); } }
sort list
public void sort(arraylist<integer> sort){ collections.sort(sort, new comparator<integer >() { @override public int compare(integer o1, integer o2) { return (o1<o2 ? -1 : (o1==o2 ? 0 : 1)); } }); }
then make call list adapter
adapter.notifydatasetchanged();
the sort method sorting in ascending order change descending change o1<o2
o1>o2
Comments
Post a Comment