Android Programming Tutorials

(Romina) #1
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
Free download pdf