i'm newbie at android programming, so please help me: I need to get the count of specific record, this my code:
public int countFiltered(String selection){
Cursor cursor = database.query(DenverDBOpenHelper.TABLE_DENVERS, denverAllColumns, selection, null, null, null, null);
Log.i(LOGTAG, "Get " + cursor.getCount() + " rows");
int countItem = cursor.getCount();
if(cursor==null) {
return 0;
}else
return countItem;
}
and i call it on the other activity:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_baby, container, false);
txtPass = (TextView)rootView.findViewById(R.id.txtPass);
int countPass = dataSource.countFiltered("score == 1");
txtPass.setText(countPass);}
countPass show NullPinterException
and my score column type is INTEGER, this my DBOpenHelper:
public class DenverDBOpenHelper extends SQLiteOpenHelper {
private static final String LOGTAG = "LitleDenver";
private static final String DATABASE_NAME = "db_denver";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_DENVERS = "denvers";
public static final String COLUMN_ID = "itemId";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESC = "description";
public static final String COLUMN_DETAIL = "detail";
public static final String COLUMN_IMAGE = "image";
public static final String COLUMN_MIN = "min";
public static final String COLUMN_MAX = "max";
public static final String COLUMN_SCORE = "score";
public static final String COLUMN_STATUS = "status";
private static final String TABLE_CREATE =
"CREATE TABLE " + TABLE_DENVERS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_CATEGORY + " TEXT, " +
COLUMN_TITLE + " TEXT, " +
COLUMN_DESC + " TEXT, " +
COLUMN_DETAIL + " TEXT, " +
COLUMN_IMAGE + " TEXT, " +
COLUMN_MIN + " INTEGER, " +
COLUMN_MAX + " INTEGER, " +
COLUMN_SCORE + " INTEGER, " +
COLUMN_STATUS + " INTEGER " +
")";