Saturday, September 17, 2011

[android-developers] Random SQLite Database Help

Hello everyone,
I came across a problem that I could really use some help with. I
created a SQLite database with five columns, _id, Favorite, Category,
Question, and Answer. I have a spinner in my layout that has the
category choices, All, Favorite, People, Places, Things, and Hard.
When All is selected pressing a button will pull a random row from the
database and this works great. My problem is when I try to grab a row
when a different category choice is selected it will only return the
first one from the list. I would like it to return a random row with
that category but I have not been able to figure it out. I would
really be happy even if it returned the next row when the query is ran
again. Thank you for the help.

Here is the code that I am having problems with.

public Cursor getRandom() throws IOException
{
String limit = "1";
int id = 1;
id = countEntries();
open();
int rand = random.nextInt(id) + 1;
if (Test.spinner.contains("All"))
{
where = KEY_ROWID + "=" + rand;
}
else
{
if (Test.spinner.contains("Favorite"))
{
where = KEY_FAV + " LIKE '" + "1" + "%'";
}
else
{
where = KEY_CAT + " LIKE '" + Test.spinner + "%'";
}
}

Cursor cursor = Db.query(DB_TABLE,null, where, null, null, null,
null, limit);
if (cursor.moveToFirst())
{
rowid = cursor.getString(0);
fav = cursor.getString(1);
cat = cursor.getString(2);
quest = cursor.getString(3);
answer = cursor.getString(4);
}

close();
return cursor;
}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment