问题
I'm trying to load my ListView from the following array
static final String[][] Bookings = new String[][] {{"WC11", "John Smith"},{"WX11", "Terry Jones"}};
I'm try to use the following code but Eclipse wont accept it.
ListView bkgList = (ListView) findViewById(R.id.bkg_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Bookings));
It says 'the constructor ArrayAdapter(ListVActivity, int, String[][]) is undefined'. How can I load my array into my ListView?
回答1:
ArrayAdapter is for a 1-d Array and the constructor expects something like String[] bookings
.
You can do something like this. Keep in mind you would need to override toString() of Booking because that is what is displayed in the TextView.
class Booking {
String type;
String name;
public Booking(String type, String name) {
this.type = type;
this.name = name;
}
}
Booking[] Bookings = new Booking[2];
Bookings[0] = new Booking("WC11", "John Smith");
Bookings[1] = new Booking("WX11", "Terry Jones");
ListView bkgList = (ListView) findViewById(R.id.bkg_list);
setListAdapter(new ArrayAdapter<Booking>(this, R.layout.list_item, Bookings));
If you want to keep the 2-d array you can extend BaseAdapter to make a custom BookingsAdapter.
回答2:
If you want more than a single TextView
per row of your ListView
you may want to consider subclassing BaseAdapter
rather than using ArrayAdapter
.
For an example of how to do this, see here
来源:https://stackoverflow.com/questions/5157883/how-to-load-a-listview-with-a-multiple-array