2013-05-03 44 views

回答

2

我不確切地知道你想要做什麼,但是你有json響應後需要幾個步驟。

  1. 使JSON數據對象的列表
  2. 排序列表
  3. 設置ListView的適配器排序列表

對於未來的問題,請您的問題分開到不同的小,但具體化的問題。

1.使JSON數據對象

對象數據模型

class YourDataModel { 
     int distance; 
     ..... 
     public int getDistance(){ 
       return this.distance; 
     } 

     public void setDistance(int distance){ 
       this.distance = distance; 
     } 
} 

的名單,然後讓我們說getJsonResponse()與列表中的所有數據返回的JSON。讓我們作出這樣的JSON名單列出:d

String json = getJsonResponse(); // Change that line with your code 
Gson gson = new Gson(); 
Type listType = new TypeToken<List<YourDataModel>>(){}.getType(); 
ArrayList<YourDataModel> data = (ArrayList<YourDataModel>) gson.fromJson(jsonOutput, listType); 

2.排序列表

現在讓我們做一些魔術:d數據是我的數組裏面的物品。我不知道d

ListView lvData = (ListView) findViewById(R.id.listview1); 
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.listview_item_row, data); 
lvData.setAdapter(adapter); 

,如果這就是:與排序列表到ListView

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.設置列表適配器

我不知道該怎麼形容這裏你想要什麼,但這是如何做到的。 如果你想直接從列表視圖中排序數據,請閱讀:Sort listview with array adapter

-1
public void sortByLocation() { 
    Collections.sort(list, new Comparator<PendingJobInfo>() { 
     @Override 
     public int compare(PendingJobInfo lhs, PendingJobInfo rhs) { 
      // TODO Auto-generated method stub 

      Double f1 = lhs.getDistance(); 
      Double f2 = rhs.getDistance(); 
      return f1.compareTo(f2); 


     } 
    }); 
}