Android How to - Get an array of all int type data matching the conditions








The following code shows how to get an array of all int type data matching the conditions.

Example

/*from   w ww. jav a 2 s . co  m*/
import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class DbUtils {

  private SQLiteDatabase mDb;
  private Context mContext;

  private static final String TAG = DbUtils.class.getSimpleName();

  /** Get an array of all int type data matching the conditions **/
  public static int[] getAllInts(SQLiteDatabase db, String table, String column) {
    Cursor c = db.query(table, new String[] { column }, null, null, null, null, column);
    if (!c.moveToFirst()) {
      Log.d(TAG, "Tried to load a int[], but there are none in " + table + "-" + column);
      c.close();
      return null;
    }
    int[] toReturn = new int[c.getCount()];
    for (int i = 0; i < c.getCount(); i++) {
      toReturn[i] = c.getInt(c.getColumnIndex(column));
      c.moveToNext();
    }
    c.close();
    return toReturn;
  }
}