listview - Android: Sort List View data -


i have json data pulling down server. 1 of fields in data distance value. need have data sorted distance lowest highest in listview. not sure how go it?

any appreciated.

i don't know want there few steps needed after have json response.

  1. make json data list of objects
  2. sort list
  3. set listview's adapter sorted list

for future questions please separate question different smaller concretized questions.

1. make json data list of objects

object data model

class yourdatamodel {        int distance;        .....        public int getdistance(){               return this.distance;        }         public void setdistance(int distance){               this.distance = distance;        } } 

then let's getjsonresponse() returns json data list. , let's make json list list :d

string json = getjsonresponse(); // change line code gson gson = new gson(); type listtype = new typetoken<list<yourdatamodel>>(){}.gettype(); arraylist<yourdatamodel> data = (arraylist<yourdatamodel>) gson.fromjson(jsonoutput, listtype); 

2. sort list

now let's magic :d data array items inside.

collections.sort(data, new comparator<yourdatamodel>() {     @override     public int compare(yourdatamodel data1, yourdatamodel data2) {           if( data1.getdistance() < data2.getdistance() )              return 1;           else              return 0;     } }); 

3. set list adapter sorted list listview

i don't know describe here :d

listview lvdata = (listview) findviewbyid(r.id.listview1); mycustomadapter adapter = new mycustomadapter(this, r.layout.listview_item_row, data); lvdata.setadapter(adapter); 

i don't know if that's want how need done. if want sort data directly listview read this: sort listview array adapter


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -