Build and Attach the Adapter
The ListView will remain empty, of course, until we do something to populate it. What we want is for the list to show our running lineup of restaurant objects.
Since we have our ArrayList<Restaurant>, we can easily wrap it in an ArrayAdapter<Restaurant>. This also means, though, that when we add a restaurant, we need to add it to the ArrayAdapter via add() – the adapter will, in turn, put it in the ArrayList. Otherwise, if we add it straight to the ArrayList, the adapter will not know about the added restaurant and therefore will not display it.
Here is the new implementation of the LunchList class:
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class LunchList extends Activity {
List<Restaurant> model=new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list=(ListView)findViewById(R.id.restaurants);
adapter=new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
Restaurant r=new Restaurant();
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types=(RadioGroup)findViewById(R.id.types);
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
r.setType("sit_down");
break;
case R.id.take_out:
r.setType("take_out");
break;
case R.id.delivery:
r.setType("delivery");
break;
}
adapter.add(r);
}
};
}
The magic value android.R.layout.simple_list_item_1 is a stock layout for a list row, just displaying the text of the object in white on a black background with a reasonably large font. In later tutorials, we will change the look of our rows to suit our own designs.
If you then add a few restaurants via the form, it will look something like this:
Since we have our ArrayList<Restaurant>, we can easily wrap it in an ArrayAdapter<Restaurant>. This also means, though, that when we add a restaurant, we need to add it to the ArrayAdapter via add() – the adapter will, in turn, put it in the ArrayList. Otherwise, if we add it straight to the ArrayList, the adapter will not know about the added restaurant and therefore will not display it.
Here is the new implementation of the LunchList class:
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import java.util.ArrayList;
import java.util.List;
public class LunchList extends Activity {
List<Restaurant> model=new ArrayList<Restaurant>();
ArrayAdapter<Restaurant> adapter=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
ListView list=(ListView)findViewById(R.id.restaurants);
adapter=new ArrayAdapter<Restaurant>(this,
android.R.layout.simple_list_item_1,
model);
list.setAdapter(adapter);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
Restaurant r=new Restaurant();
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types=(RadioGroup)findViewById(R.id.types);
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
r.setType("sit_down");
break;
case R.id.take_out:
r.setType("take_out");
break;
case R.id.delivery:
r.setType("delivery");
break;
}
adapter.add(r);
}
};
}
The magic value android.R.layout.simple_list_item_1 is a stock layout for a list row, just displaying the text of the object in white on a black background with a reasonably large font. In later tutorials, we will change the look of our rows to suit our own designs.
If you then add a few restaurants via the form, it will look something like this:
Our LunchList with a few fake restaurants added

No comments:
Post a Comment