Android Record Count Toast Message? - android

I have an App I would like to cripple by allowing only 5 records to be shown when doing a query.... The following is the code I'm using within the DataBaseHelper.java for this and it works great for what I need........It returns only 5 records in DESC order by KEY_RECORD1
My question is I would like implement a Warning message in the form of a Toast when this 5 Record limit it reached.....
So when you Add the Sixth Record you would get....Example Toast: Limit of 5 Records has been reached!
public Cursor fetchAllNotes() {
return mDb.query(DATABASE_TABLE,
new String[] { KEY_ROWID, KEY_RECORD1, KEY_RECORD2,
KEY_RECORD3}, null,
null, null, null, KEY_RECORD1 + " DESC", "5");
}

if (fetchAllNotes().getCount() >= 5){
//Toast.makeText...
}
to avoid extra request and improve perfs, you could do that once, to retrieve the number of records, and increment that counter each time you do an insertion.

Related

one query for two purposes

I have the following two queries:
A.
db.query(DBHelper.TABLE, new String[] {
DBHelper._ID, DBHelper._DATE_AND_TIME,
DBHelper._SOURCE, DBHelper._MODE,
"SUM(" + DBHelper._AMOUNT + ")" },
DBHelper._DATE_AND_TIME + " BETWEEN ? AND ?",
new String[] { date_min, date_max }, null, null, null, null);
and result of sum goes to textview like this
String.valueOf(cursor.getString(4)).
Second query B.
db.query(DBHelper.TABLE, new String[] {
DBHelper._ID, DBHelper._DATE_AND_TIME,
DBHelper._SOURCE, DBHelper._MODE,
DBHelper._AMOUNT }, DBHelper._DATE_AND_TIME
+ " BETWEEN ? AND ?", new String[] { date_min, date_max },
null, null, null, null);
and result goes to
adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from,
to, FLAG_AUTO_REQUERY);
What I want is to combine both queries. Close cursor after first query and to use same query (A) for adapter. So far I have added DBHelper._AMOUNT to SELECT of A query but ListView shows only the last entry result (not the whole data). How can I modify query A for showing SUM in TextView and then use same query for adapter.
In a normal query (like B), the database returns a result record for each table record that matches the WHERE filter.
However, when you are using an aggregate function like SUM(), the database computes a single value from all table records (or from all records in a group if you're using GROUP BY) and returns that as a single result record.
Therefore, your two queries are fundamentally different and cannot be combined into a single query.
(Please note that the first four result columns of your query A do not have any meaningful value because the result record is not guaranteed to correspond to any particular record in the original table; you should ask only for the SUM value.)

query the latest SMS messages group by contact

Like the Message app, the first Activity displays the lastest SMS grouped by person and the count number.
Example I had exchanged 50 texts with Chris, 60 with Aline, 40 with Ray ...
The app displays something like this
Chris (50) Hey how are you lately ?
Aline (60) Let's catch up
Ray (40) Here is my number
Ethan (1) I wrote a solution
I'm trying to do the same . I query all the SMS then I sort them.
At least it's O(n) efficient .
Cursor cur = this.getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
int count = cur.getCount();
// iterate all the SMS
for (int i=1 ; i<=count ; i++){
// processing
....
cur.moveToNext();
}
Is there a way to query the latest messages (received or sent no draft type) grouped by person and get the number of SMS from a person ?
I suppose there are multiple queries.
I think this will do what you want:
Cursor cur = this.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" );
The returned cursor will contain the latest message in every conversation and the amount of messages in each conversation.

Android cursor fetch understanding

I would like to understand a line in a piece of code I saw:
public Cursor fetchMessageByMessageId(String msgId) {
Cursor mCursor =
mDb.query(true, DATABASE_MESSAGES_TABLE, new String[] { KEY_ROWID,
KEY_CONVERSATION_ID, KEY_MSG_ID, KEY_TITLE, KEY_BODY,
KEY_IS_REPLY, KEY_MEDIA_LOC, KEY_URL, KEY_TIMESTAMP },
KEY_MSG_ID + "='" + msgId + "'", null, null, null, null, null);
**if (mCursor != null) {
mCursor.moveToFirst();
}**
return mCursor;
}
The following lines of code in between **
Is this line necessary? I spent 2 hours debugging today finding out why my data was missing when I called something like
while(mCursor.moveTonext())
use the cursor to grab some data and ended up missing the first data always. So I looked at my other parts of the code and realised that I dumped the whole cursor into the adapter so the bold line above had no effect whatsoever. After removing those lines of code everything was good!
So in short, if I just want a cursor with 1 result or many, is it necessary to call the bold statement above? Thanks!
Basically, you get a set of row when you call query(). Initially the cursor will be pointing to nothing. If you call mCursor.moveToNext() or mCursor.moveToFirst(), then you will want to point the the first row. Additional calls to mCursor.moveToNext() will move the cursor to next tuple.
In short, only call mCursor.moveToNext() when you need to get information from next row (if it exists).

Android query vs rawQuery results

When I run either of the queries below with a normal 13 digit barcode a result is returned from my database perfectly fine. However when I run the exact same code with an 8 digit barcode the result is not found in the second query even though it exists in the DB. To me the queries look identical. What could be going wrong?
return mDb.rawQuery("SELECT * FROM `products` WHERE bcode = '"+bcode+"'", null);
return mDb.query(DATABASE_PRODUCT_TABLE,new String[] { KEY_ROWID, KEY_NAME, KEY_BCODE,KEY_USEBY_DAYS}, KEY_BCODE + " = " + bcode, null, null,null, null, null);
Does the 8-digit barcode have leading zeroes, by any chance? Because WHERE clause in the second example is wrong.

how to retrieve particular records from SQLite in android

I need to get data from DB & displayed it as list with pagination.
i.e. If i retrieved 4 items..i need to display first 2 items in first page.
When i click next button.,remaining 2 items should be displayed which replaces old 2.
How could i restrict data from DB as 2 like that?
My code..
db.open();
// db.insertTitle("Money");
//db.insertTitle("make");
//db.insertTitle("make");
//db.insertTitle("make");
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
String firstName = c.getString(c.getColumnIndex("user"));
results.add( firstName );
} while (c.moveToNext());
}
setListAdapter(new ArrayAdapter<String>(DisplayAll.this, android.R.layout.simple_list_item_1,results));
db.close();
}
My DBAdapter..
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[]
{
KEY_ROWID,
KEY_USER,
},
null,
null,
null,
null,
null);
}
Please try this
public Cursor getAllRecords(int page,int totalRecord)
{
return db.rawQuery("select * from your_table_name limit "+(page-1)*totalRecord+","+totalRecord, null);
}
Where limit = how many record you want at a time if you want 2 record then pass limit = 2 if 10 record then set limit = 10..
and page = first initial page variable with 1 and when second time you fetch next record increase your page variable by 1 .

Categories

Resources