2012-02-10 45 views
0

我想從光標的值而不SimpleCursorAdapter part.here是代碼的Android的SQLite獲得從數據庫中的數據到陣列

public Cursor queueAll(){ 
    String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2,KEY_CONTENT3}; 
    Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
    null, null, null, null, null); 
    return cursor; 
} 

和活動側代碼

cursor = mySQLiteAdapter.queueAll(); 

     from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3}; 
     int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3}; 
     cursorAdapter = 
     new SimpleCursorAdapter(this, R.layout.row, cursor, from, to); 
     listContent.setAdapter(cursorAdapter); 

     while(!cursor.isAfterLast()) 
     { 
      String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1)); 
      String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3)); 
      if(tilt.equals("LEFT")) 
      { 
      Log.v("LEFT",pkg); 
      } 
      else if(tilt.equals("RIGHT")) 
      { 
       Log.v("RIGHT",pkg); 
      } 
      cursor.moveToNext(); 
     } 

我我得到的pkg值correct.but我想直接從光標獲取值,同時刪除SimpleCursorAdapter部分代碼不起作用。 任何幫助將不勝感激:)

+0

它不清楚你在這裏找什麼。 – 2012-02-10 11:28:29

+0

我想從光標的值。但沒有這段代碼=新的String [] {SQLiteAdapter.KEY_ID,SQLiteAdapter.KEY_CONTENT1,SQLiteAdapter.KEY_CONTENT2,SQLiteAdapter.KEY_CONTENT3}; int [] to = new int [] {R.id.id,R.id.text1,R.id.text2,R.id.text3}; cursorAdapter = new SimpleCursorAdapter(this,R.layout.row,cursor,from,to); listContent.setAdapter(cursorAdapter); – 2012-02-10 11:31:02

+0

你必須在進入循環之前調用cursor.moveToFirst()。 (或使用moveToNext()的結果作爲退出條件 – njzk2 2012-02-10 12:04:06

回答

0

您可以獲取值,而無需聲明適配器。如果您想要在列表窗口小部件中顯示數據,則需要適配器。所以你可以如下:

cursor = mySQLiteAdapter.queueAll(); 

if (cursor == null) { 
//check if there are errors or query just return null 
} 
if (cursor.moveToFirst()) { 
     while(!cursor.isAfterLast()) 
     { 
      String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1)); 
      String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3)); 
      if(tilt.equals("LEFT")) 
      { 
      Log.v("LEFT",pkg); 
      } 
      else if(tilt.equals("RIGHT")) 
      { 
       Log.v("RIGHT",pkg); 
      } 
      cursor.moveToNext(); 
     } 
} 
相關問題