2012-02-10 11 views
0

にデータベースからデータを取得私はSimpleCursorAdapterのpart.hereなしカーソルから値を取得するコードアンドロイド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値を取得しています。しかし、コードが機能しないSimpleCursorAdapter部分を削除しながら、カーソルから値を直接取得したいです。 助けていただければ幸いです。

+0

ここであなたが探しているものは明確ではありません。 –

+0

このコードを挿入せずに、cursor.butから値を取得したいと思います。= new String [] {SQLiteAdapter.KEY_ID、SQLiteAdapter.KEY_CONTENT1、SQLiteAdapter.KEY_CONTENT2、SQLiteAdapter.KEY_CONTENT3}; int [] to =新しいint [] {R.id.id、R.id.text1、R.id.text2、R.id.text3}; cursorAdapter = 新しいSimpleCursorAdapter(this、R.layout.row、cursor、from、to); listContent.setAdapter(cursorAdapter); –

+0

ループに入る前にcursor.moveToFirst()を呼び出さなければなりません。 (またはmoveToNext()の結果を終了条件として使用する – njzk2

答えて

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(); 
     } 
}