I am creating task manager. I have tasklist and I want when I click on particular tasklist name if it empty then it goes on Add Task activity but if it has 2 or 3 tasks then it shows me those tasks into it in list form.
I am trying to get count in list. my database query is like:
public Cursor getTaskCount(long tasklist_Id) { SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor= db.rawQuery("SELECT COUNT (*) FROM " + TABLE_TODOTASK + " WHERE " + KEY_TASK_TASKLISTID + "=?", new String[] { String.valueOf(tasklist_Id) }); if(cursor!=null && cursor.getCount()!=0) cursor.moveToNext(); return cursor; }
In My activity:
list_tasklistname.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, android.view.View v, int position, long id) { db = new TodoTask_Database(getApplicationContext()); Cursor c = db.getTaskCount(id); System.out.println(c.getCount()); if(c.getCount()>0) { System.out.println(c); Intent taskListID = new Intent(getApplicationContext(), AddTask_List.class); task = adapter.getItem(position); int taskList_id = task.getTaskListId(); taskListID.putExtra("TaskList_ID", taskList_id); startActivity(taskListID); } else { Intent addTask = new Intent(getApplicationContext(), Add_Task.class); startActivity(addTask); } } }); db.close(); }
but when I am clicking on tasklist name it is returning 1, bot number of tasks into it.
SQLite Count(*) Function In SQLite the Count(*) function will return total number of rows available in a table, including the rows which contain NULL values. The Count(*) will not take any parameters other than the asterisk symbol (*).
The theoretical maximum number of rows in a table is 264 (18446744073709551616 or about 1.8e+19). This limit is unreachable since the maximum database size of 281 terabytes will be reached first.
Using DatabaseUtils.queryNumEntries()
:
public long getProfilesCount() { SQLiteDatabase db = this.getReadableDatabase(); long count = DatabaseUtils.queryNumEntries(db, TABLE_NAME); db.close(); return count; }
or (more inefficiently)
public int getProfilesCount() { String countQuery = "SELECT * FROM " + TABLE_NAME; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int count = cursor.getCount(); cursor.close(); return count; }
In Activity:
int profile_counts = db.getProfilesCount(); db.close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With