Android中的排序列表视图数据
我有一些JSON数据,我正在从服务器上取下来。此数据中的一个字段是距离值。我需要在ListView中按从最低到最高的距离对数据进行排序。我不知道该怎么做?我不知道您到底想做什么,但在获得json响应后,需要执行的步骤很少Android中的排序列表视图数据,android,sorting,listview,Android,Sorting,Listview,我有一些JSON数据,我正在从服务器上取下来。此数据中的一个字段是距离值。我需要在ListView中按从最低到最高的距离对数据进行排序。我不知道该怎么做?我不知道您到底想做什么,但在获得json响应后,需要执行的步骤很少 将json数据添加到对象列表中 对列表排序 使用排序列表设置listview的适配器 对于未来的问题,请将您的问题分为不同的较小但具体化的问题 1。将json数据添加到对象列表中 对象数据模型 class YourDataModel { int distance;
class YourDataModel {
int distance;
.....
public int getDistance(){
return this.distance;
}
public void setDistance(int distance){
this.distance = distance;
}
}
然后假设getJsonResponse()返回json以及列表中的所有数据。让我们将json列表设置为list: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);
我不知道这是否是你想要的,但这就是需要做的。
如果要直接从listview对数据进行排序,请阅读以下内容:public void sortByLocation(){
Collections.sort(list,newcomparator(){
@凌驾
公共整数比较(PendingJobInfo lhs、PendingJobInfo rhs){
//TODO自动生成的方法存根
双f1=lhs.getDistance();
双f2=rhs.getDistance();
返回f1.compareTo(f2);
}
});
}
使用此选项,您就完成了:
private void sortAscending() {
Collections.sort(productList, new AscendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "sortAscending" + productList);
/*for (ProductModel s : productList){
Log.d("My array list content: ", s.getPrice()+"::"+s.getName());
}*/
}
private void sortDescending() {
Collections.sort(productList, new DescendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "productList" + productList);
for (ProductModel s : productList) {
Log.d("My array list content: ", s.getPrice() + "::" + s.getName());
}
}
public class AscendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM1 > pM2){
return 1;
}else if((pM1 < pM2)){
return -1;
}else{
return 0;
}
}
}
public class DescendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
//Log.d("hightolow","productList"+p2.getPrice().compareTo(p1.getPrice()));
// Log.d("hightolow","productList:"+p1.getPrice());
// Log.d("hightolow","productList::"+p2.getPrice());
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM2 > pM1){
return 1;
}else if((pM2 < pM1)){
return -1;
}else{
return 0;
}
}
}
private void排序集(){
Collections.sort(productList,new AscendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d(“hightolow”、“sortAscending”+产品列表);
/*对于(产品型号s:productList){
Log.d(“我的数组列表内容:”,s.getPrice()+“:”+s.getName());
}*/
}
私有无效排序结果(){
Collections.sort(productList,new degeneratingcomparator());
rcAdapter.notifyDataSetChanged();
Log.d(“hightolow”、“productList”+productList);
对于(产品型号s:productList){
Log.d(“我的数组列表内容:”,s.getPrice()+“:”+s.getName());
}
}
公共类AscendingComparator实现了Comparator{
@凌驾
公共整数比较(ProductModel p1、ProductModel p2){
int pM1=Math.round(Float.parseFloat(p1.getPrice());
int pM2=Math.round(Float.parseFloat(p2.getPrice());
如果(pM1>pM2){
返回1;
}否则如果((pM1pM1){
返回1;
}否则如果((pM2
ListView lvData = (ListView) findViewById(R.id.listview1);
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.listview_item_row, data);
lvData.setAdapter(adapter);
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);
}
});
}
private void sortAscending() {
Collections.sort(productList, new AscendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "sortAscending" + productList);
/*for (ProductModel s : productList){
Log.d("My array list content: ", s.getPrice()+"::"+s.getName());
}*/
}
private void sortDescending() {
Collections.sort(productList, new DescendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "productList" + productList);
for (ProductModel s : productList) {
Log.d("My array list content: ", s.getPrice() + "::" + s.getName());
}
}
public class AscendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM1 > pM2){
return 1;
}else if((pM1 < pM2)){
return -1;
}else{
return 0;
}
}
}
public class DescendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
//Log.d("hightolow","productList"+p2.getPrice().compareTo(p1.getPrice()));
// Log.d("hightolow","productList:"+p1.getPrice());
// Log.d("hightolow","productList::"+p2.getPrice());
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM2 > pM1){
return 1;
}else if((pM2 < pM1)){
return -1;
}else{
return 0;
}
}
}