A Restaurant In Your Own Home
.getReadableDatabase()
.rawQuery("SELECT COUNT(*) FROM restaurants", null);
c.moveToFirst();
int count=c.getInt(0);
c.close();
if (count>0) {
int offset=(int)(count*Math.random());
String args[]={String.valueOf(offset)};
c=helper
.getReadableDatabase()
.rawQuery("SELECT name FROM restaurants LIMIT 1 OFFSET ?", args);
c.moveToFirst();
updateViews.setTextViewText(R.id.name, c.getString(0));
}
else {
updateViews.setTextViewText(R.id.name,
ctxt.getString(R.string.empty));
}
}
finally {
helper.close();
}
mgr.updateAppWidget(me, updateViews);
}
Here, we:
•
Create a RemoteViews object, which represents a set of GUI
"commands" to invoke on the home screen that defines how to
modify the app widget
•
Open up a database connection
•
Find out how many restaurants there are via a SQL query
•
Load a random restaurant via another SQL query
•
Set the name TextView in the app widget (via the RemoteViews) to have
either the name of the restaurant or an error message
•
Update the app widget itself
322
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition
.getReadableDatabase()
.rawQuery("SELECT COUNT(*) FROM restaurants", null);
c.moveToFirst();
int count=c.getInt(0);
c.close();
if (count>0) {
int offset=(int)(count*Math.random());
String args[]={String.valueOf(offset)};
c=helper
.getReadableDatabase()
.rawQuery("SELECT name FROM restaurants LIMIT 1 OFFSET ?", args);
c.moveToFirst();
updateViews.setTextViewText(R.id.name, c.getString(0));
}
else {
updateViews.setTextViewText(R.id.name,
ctxt.getString(R.string.empty));
}
}
finally {
helper.close();
}
mgr.updateAppWidget(me, updateViews);
}
Here, we:
•
Create a RemoteViews object, which represents a set of GUI
"commands" to invoke on the home screen that defines how to
modify the app widget
•
Open up a database connection
•
Find out how many restaurants there are via a SQL query
•
Load a random restaurant via another SQL query
•
Set the name TextView in the app widget (via the RemoteViews) to have
either the name of the restaurant or an error message
•
Update the app widget itself
322
Subscribe to updates at http://commonsware.com
Special Creative Commons BY-NC-SA 3.0 License Edition
