2013-05-03 43 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); 


     } 
    }); 
}