Search code examples
javaandroidcursor

java - dynamic casting using strings


Consider a sqlite database table DRINK with columns: Name(String), Description(String), ResourceID(int).

Now, after I get a Cursor cur everytime I'd like to get something out of a particular row, I'd have to do something like:

cur = db.query(...);
String name = cur.getString(0);
String desc = cur.getString(1);
int desc = cur.getInt(0);

But, this logic is so dependent on my knowledge of knowing the database table schema and I'd like to avoig that. What I'd like to do is something like this:

Object x = cur.getObject(0); //not available

but, as marked the getObject is not available to a cursor

but, I also require while doing this the data types are also preserved, meaning, a 1 stored as integer in its ResourceID column, does not give 1 instanceOf String=true, it should give 1 instanceOf int=true.

so, why would I want to do that? If I can do this, then I can obtain values from the database in something like a for loop without having the worry what is in the schema and providing only it's length.

What are the ways this can be achieved, or atleast achieving a similar behaviour.


Solution

  • Call getType() then use a switch statement to call the appropriate getXxx() method.