Android Programming Tutorials

(Romina) #1
More Home Cooking

c.close();

if (count> 0 ) {
int offset=(int)(count*Math.random());
String args[]={String.valueOf(offset)};

c=helper
.getReadableDatabase()
.rawQuery("SELECT _ID, name FROM restaurants LIMIT 1 OFFSET ?", args);
c.moveToFirst();
updateViews.setTextViewText(R.id.name, c.getString( 1 ));

Intent i=new Intent(this, DetailForm.class);

i.putExtra(LunchList.ID_EXTRA, c.getString( 0 ));

PendingIntent pi=PendingIntent.getActivity(this, 0 , i,
PendingIntent.FLAG_UPDATE_CURREN
T);

updateViews.setOnClickPendingIntent(R.id.name, pi);
}
else {
updateViews.setTextViewText(R.id.title,
this.getString(R.string.empty));
}
}
finally {
helper.close();
}

Intent i=new Intent(this, WidgetService.class);
PendingIntent pi=PendingIntent.getService(this, 0 , i, 0 );

updateViews.setOnClickPendingIntent(R.id.next, pi);
mgr.updateAppWidget(me, updateViews);
}

You will notice a few changes:



  • We now get the _ID column out of our content provider, in addition
    to the name, and we adjust the code that fills in the name widget's


text to match.



  • We create an activity PendingIntent for the DetailForm activity.
    However, we add in the ID_EXTRA "extra", so DetailForm knows which


restaurant to show. And, since we are changing extras on an
otherwise-unchanging Intent, we need to add the

FLAG_UPDATE_CURRENT flag in our getActivity() call, so the new extra
takes effect

332
Free download pdf