【问题标题】:How to set the arraylist item to be add in to reverse order?如何设置要添加的arraylist项目以相反的顺序?
【发布时间】:2011-11-12 23:12:04
【问题描述】:

在我的应用程序中,我将使用以下代码存储数据库中所有可用表的名称:

     public ArrayList<Object> showAllTable()
  {
      ArrayList<Object> tableList = new ArrayList<Object>();

      String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
      Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
      cursor.moveToFirst();

      if (!cursor.isAfterLast()) 
      {
          do 
          {
              if(cursor.getString(0).equals("android_metadata"))
              {
                  //System.out.println("Get Metadata");
                  continue;
              }
              else
              {
                  tableList.add(cursor.getString(0));
              }
          }
          while (cursor.moveToNext());
      }
      cursor.close();

      return tableList;

  }

是的,我可以存储它。但是当我将它显示到 ListView 中时,它会以最后创建的方式显示。我想将表格列表显示为上次创建的,首先显示在 ListView 中。

请帮助我,我应该在我的代码中做些什么来设置这种显示表格的方式?

【问题讨论】:

    标签: android database android-layout android-emulator android-widget


    【解决方案1】:

    有几种方法可以做到这一点。最简单的可能是通过添加ORDER BY 来更改您的SQL 调用。您可以尝试以下方法:

    按升序排序:

    String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC";
    

    或降序:

    String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name DESC";
    

    替代解决方案

    另一种选择是像现在一样进行操作,而是从最后一项到第一项遍历 Cursor。这是一些伪代码:

    if (cursor.moveToLast()) {
       while (cursor.moveToPrevious()) {
          // Add the Cursor data to your ArrayList
       }
    }
    

    【讨论】:

    • 谢谢。这对我很有帮助。
    【解决方案2】:

    在返回值之前调用方法

    Collections.reverse(tableList);
    

    查看完整代码

    public ArrayList<Object> showAllTable()
    
    { 
     ArrayList tableList = new ArrayList();
    
     String SQL_GET_ALL_TABLES = "SELECT name FROM sqlite_master WHERE type='table'";
     Cursor cursor = db.rawQuery(SQL_GET_ALL_TABLES, null);
     cursor.moveToFirst();
    
     if (!cursor.isAfterLast()) 
     {
      do 
      {
          if(cursor.getString(0).equals("android_metadata"))
          {
              //System.out.println("Get Metadata");
              continue;
          }
          else
          {
              tableList.add(cursor.getString(0));
          }
      }
      while (cursor.moveToNext());
     }
     cursor.close();
     Collections.reverse(tableList);
     return tableList;
    }
    

    【讨论】:

    • 感谢您的回复。但 Michell Bak 的回答效果很好。也谢谢你。
    • 最欢迎亲爱的.. 但是一件小事,如果你使用 "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name ASC" ,这会按升序返回名称,而不是插入订单。例如,首先我创建了名为“tableone”、“tabletwo”、“tablethree”和“tablefour”的表。查询返回结果为“'tablefour','tableone','tablethree' and 'tabletwo'”所以结果不是插入顺序或倒序。请检查一下。不要消极对待。只是我在详细解释。
    • 是的,我知道。这就是为什么我使用 Michell Bak 在他的回答中给出的替代解决方案。它可以按我的意愿工作。
    • 感谢您的解释。
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多