Change our Adapter and Wrapper
Of course, our existing RestaurantAdapter extends ArrayAdapter and cannot use a Cursor very effectively. So, we need to change our RestaurantAdapter into something that can use a Cursor...such as a CursorAdapter. Just as an ArrayAdapter creates a View for every needed item in an array or List, CursorAdapter creates a View for every needed row in a Cursor.
A CursorAdapter does not use getView(), but rather bindView() and newView(). The newView() method handles the case where we need to inflate a new row; bindView() is when we are recycling an existing row. So, our current getView() logic needs to be split between bindView() and newView().
Replace our existing RestaurantAdapter implementation in LunchList with the following:
class RestaurantAdapter extends CursorAdapter {
RestaurantAdapter(Cursor c) {
super(LunchList.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
RestaurantHolder holder=(RestaurantHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
RestaurantHolder holder=new RestaurantHolder(row);
row.setTag(holder);
return(row);
}
}
Then, you need to make use of this refined adapter, by changing the model in LunchList from an ArrayList to a Cursor. After you have changed that data 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;
RestaurantHolder(View 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);
}
}
}
A CursorAdapter does not use getView(), but rather bindView() and newView(). The newView() method handles the case where we need to inflate a new row; bindView() is when we are recycling an existing row. So, our current getView() logic needs to be split between bindView() and newView().
Replace our existing RestaurantAdapter implementation in LunchList with the following:
class RestaurantAdapter extends CursorAdapter {
RestaurantAdapter(Cursor c) {
super(LunchList.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
RestaurantHolder holder=(RestaurantHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
RestaurantHolder holder=new RestaurantHolder(row);
row.setTag(holder);
return(row);
}
}
Then, you need to make use of this refined adapter, by changing the model in LunchList from an ArrayList to a Cursor. After you have changed that data 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;
RestaurantHolder(View 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);
}
}
}
No comments:
Post a Comment