【问题标题】:Read from a database in android to an arraylist从android中的数据库读取到arraylist
【发布时间】:2011-10-14 18:36:56
【问题描述】:

我是 android 开发的新手,不知道如何从我的database/table 读取到 arrayList。现在我有:

      Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

       int Column1 = c.getColumnIndex("Field1");
       int Column2 = c.getColumnIndex("Field2");

       // Check if our result was valid.
       c.moveToFirst();
       if (c != null) {
        // Loop through all Results
        do {
         String Name = c.getString(Column1);
         int Age = c.getInt(Column2);
         Data =Data +Name+"/"+Age+"\n";
        }while(c.moveToNext());
       }
       TextView tv = new TextView(this);
       tv.setText(Data);
       setContentView(tv);
      }
      catch(Exception e) {
       Log.e("Error", "Error", e);
      } finally {
       if (myDB != null)
        myDB.close();
      }

如何将其读入数组列表?

【问题讨论】:

    标签: android sqlite arraylist


    【解决方案1】:

    这是一个代码,它向您展示如何从数据库查询中获取 ArrayList。

    public ArrayList<String> GetAllValues(String aTable,String[] aColumn)
    {
        ArrayList<String> list = new ArrayList<String>();
        Cursor cursor = sqlDB.query(aTable, aColumn, null, null, null, null, null);
        if (cursor.moveToFirst())
        {
            do
            {
                list.add(cursor.getString(0));
            }
            while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) 
        {
            cursor.close();
        }
    
        return list;
    }
    

    如果您在“aColumn”中提供 null,它将为您提供所有列。我希望它会有所帮助。

    【讨论】:

      【解决方案2】:
      //ArrayList of Calllog class objects
      ArrayList<CallLog> arrCallLogs = null;
      Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
      
                  if(curCalllog != null && curCalllog.moveToFirst()) {
                  arrCallLogs = new ArrayList<CallLog>();
                  while (curCalllog.isAfterLast() == false) {
                      //Calllog is a class with list of fileds 
                      CallLog callLog = new CallLog();
                      callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
                      callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
                      callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
                      callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
                      callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
                      callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
                      callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
                      callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
                      arrCallLogs.add(callLog);
                      curCalllog.moveToNext();
                  }
              }
              curCalllog.close();
          myDB.close();
      

      这是如何从 Cursor 创建 arrayList 的示例。

      这里有一个类名 Calllog,其中包含字段列表以及 setter 和 getter 方法。 在这里,我使用 Cursor 制作了一个 Calllog 对象的 ArrayList

      【讨论】:

        猜你喜欢
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-29
        相关资源
        最近更新 更多