如何排序listview项目在android中明智的人口

时间:2015-12-08 04:49:38

标签: java android

这是我的活动

    public class MainActivity extends Activity   {

    // Declare Variables
    ListView list;
    ListViewAdapter adapter;
    EditText editsearch;
    String[] rank;
    String[] country;
    String[] population;
    ArrayList<WorldPopulation> arraylist = new ArrayList<WorldPopulation>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);

        // Generate sample data
        rank = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

        country = new String[] { "China", "India", "United States",
                "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
                "Russia", "Japan" };

        population = new String[] { "1,354,040,000", "1,210,193,422",
                "315,761,000", "237,641,326", "193,946,886", "182,912,000",
                "170,901,000", "152,518,015", "143,369,806", "127,360,000" };

        // Locate the ListView in listview_main.xml
        list = (ListView) findViewById(R.id.listview);

        for (int i = 0; i < rank.length; i++) 
        {
            WorldPopulation wp = new WorldPopulation(rank[i], country[i],
                    population[i]);
            // Binds all strings into an array
            arraylist.add(wp);
        }

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(this, arraylist);

        // Binds the Adapter to the ListView
        list.setAdapter(adapter);


    }

}

这是我的 Datamodel 类:

public class WorldPopulation {
    private String rank;
    private String country;
    private String population;

    public WorldPopulation(String rank, String country, String population) {
        this.rank = rank;
        this.country = country;
        this.population = population;
    }

    public String getRank() {
        return this.rank;
    }

    public String getCountry() {
        return this.country;
    }

    public String getPopulation() {
        return this.population;
    }
}

我在Listview中打印数据,排名,国家和人口但是我无法根据人口明智排序目前它显示排名明智而我必须打印数据人口明智的升序意味着小人口将先来然后大人口并分别请求帮助。

4 个答案:

答案 0 :(得分:2)

要么创建一个可以比较对象的Comparator,或者如果它们都是同一个类的实例,则可以使该类实现Comparable。然后,您可以使用Collections.sort()进行实际排序。

请检查以下代码,这些内容不是根据您的需要进行调整,但您可以参考。为什么不用arraylist而不是数组来保存你的数据。

ArrayList arrayList = new ArrayList();

//Add elements to Arraylist
arrayList.add("1");
arrayList.add("3");
arrayList.add("5");
arrayList.add("2");
arrayList.add("4");


Collections.sort(arrayList);

//display elements of ArrayList
System.out.println("ArrayList elements after sorting in ascending order : ");
for(int i=0; i<arrayList.size(); i++)
    System.out.println(arrayList.get(i));

Collection.sort方法将按升序排序 或创建custorm运算符类

import java.util.Comparator;

class IgnoreCaseComparator implements Comparator<String> {
  public int compare(String strA, String strB) {
    return strA.compareToIgnoreCase(strB);
  }
}

然后你的排序:

IgnoreCaseComparator icc = new IgnoreCaseComparator();

java.util.Collections.sort(arrayList,icc);

答案 1 :(得分:1)

这是因为您已将群体声明为字符串。 尝试长数据类型。

答案 2 :(得分:1)

按照此代码对任何ArrayList

进行排序
 Collections.sort(empList, new Comparator<WorldPopulation>(){
 public int compare(WorldPopulation wp1, WorldPopulation wp2) {
 return emp1.getFirstName().compareToIgnoreCase(emp2.getFirstName());
}
});

通知您的适配器数据集已更改 notifyDataSetChanged()

答案 3 :(得分:0)

这是比较器的一个简单示例。在设置adatpter之前和调用适配器构造函数之后,您将在主类中调用它。在你的情况下,因为population是一个String所以在你的比较器中你必须在int中转换它以应用排序。你必须为逗号做点什么。根据您的要求修改吹码。

 adapter.sort(new Comparator< WorldPopulation >() {
        @Override
        public int compare(WorldPopulation lhs, WorldPopulation rhs) {
            return Integer.parseInt(lhr.getgetPopulation())-Integer.parseInt(rhs.getPopulation());   //or whatever your sorting algorithm
        }
    });