【问题标题】:Showing data from bubble sort in ListView Android在 ListView Android 中显示冒泡排序的数据
【发布时间】:2015-09-14 05:52:54
【问题描述】:

我尝试使用冒泡排序从 Web 服务中对数据库进行排序。

itemLists[i] = new ItemList(
                            a.getString(id),
                            a.getString(nama),
                            a.getString(latitude),
                            a.getString(longitude),
                            a.getString(alamat));
                    double terendah = Double.valueOf(a.getString("TAG_TERENDAH")).doubleValue();
                    //double terendah = a.getDouble(terenda);
                    harga[i] = terendah;
                    //bubble sort
                    double tHarga;
                    ItemList tItemList;

                    for (int k = 0; k < tempatrental.length(); k++) {
                        for (int l = 0; l < tempatrental.length() - (k + 1); l++) {
                            if (harga[l] > harga[l + 1]) {
                                tHarga = harga[l];
                                tItemList = itemLists[l];


                                harga[l] = harga[l + 1];
                                itemLists[l] = itemLists[l + 1];


                                harga[l + 1] = tHarga;
                                itemLists[l + 1] = tItemList;

                            }

                        } 

无论如何,来自 Web 服务的数据库是数组类型,所以我将它存储在另一个类的数组中。

public class ItemList{

public String label1, label2, label3, label4, label5;

public ItemList(String label1, String label2, String label3, String label4, String label5) {
    super();
    // TODO Auto-generated constructor stub
    //this.icon = icon;
    this.label1 = label1;
    this.label2 = label2;
    this.label3 = label3;
    this.label3 = label4;
    this.label3 = label5;

}

当我尝试在列表视图中显示排序结果时出现问题。

this.setListAdapter (new ArrayAdapter<String>(TermurahActivity.this, android.R.layout.simple_list_item_1, itemLists));

Eclipse 说:构造函数 ArrayAdapter(TermurahActivity, int, ItemList[]) 未定义。请帮助解决这个问题...

【问题讨论】:

  • 为什么是BubbleSort,这是效率最低的排序算法?你不能试试快速排序吗?或者,更好的是,使用集合对象提供的 Java native sorting 方法?

标签: android listview bubble-sort


【解决方案1】:

一旦你定义了ArrayAdapter&lt;String&gt;,适配器就需要String的列表。

ItemListsItemList 的列表,因此您应该使用:

  new ArrayAdapter<ItemList>(TermurahActivity.this, 
  android.R.layout.simple_list_item_1, itemLists);`

【讨论】:

    猜你喜欢
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2013-09-28
    • 2013-10-09
    相关资源
    最近更新 更多