Android Programming Tutorials

(Romina) #1
The Restaurant Store

member, replace the current onCreate() code that populates our


RestaurantAdapter with the following:


model=helper.getAll();
startManagingCursor(model);
adapter=new RestaurantAdapter(model);
list.setAdapter(adapter);

After getting the Cursor from getAll(), we call startManagingCursor(), so


Android will deal with refreshing its contents if the activity is paused and


resumed. Then, we hand the Cursor off to the RestaurantAdapter.


Also, you will need to import android.content.Context and


android.widget.CursorAdapter in LunchList.


Then, we need to update RestaurantHolder to work with Cursor objects


rather than a restaurant directly. Replace the existing implementation with


the following:


static class RestaurantHolder {
private TextView name=null;
private TextView address=null;
private ImageView icon=null;
private View row=null;

RestaurantHolder(View row) {
this.row=row;

name=(TextView)row.findViewById(R.id.title);
address=(TextView)row.findViewById(R.id.address);
icon=(ImageView)row.findViewById(R.id.icon);
}

void populateFrom(Cursor c, RestaurantHelper helper) {
name.setText(helper.getName(c));
address.setText(helper.getAddress(c));

if (helper.getType(c).equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);
}
else if (helper.getType(c).equals("take_out")) {
icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}

100
Free download pdf