如何加载一个ListView具有多阵列 [英] How to load a ListView with a multiple array
问题描述
我想从以下数组加载我的ListView
I'm trying to load my ListView from the following array
static final String[][] Bookings = new String[][] {{"WC11", "John Smith"},{"WX11", "Terry Jones"}};
我尝试使用以下code,但Eclipse中不会接受它。
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));
它说'构造ArrayAdapter(ListVActivity,INT的String [] [])是不明确的。如何将我的数组加载到我的ListView?
It says 'the constructor ArrayAdapter(ListVActivity, int, String[][]) is undefined'. How can I load my array into my ListView?
推荐答案
ArrayAdapter是一维数组和构造函数需要像的String []预定
。
ArrayAdapter is for a 1-d Array and the constructor expects something like String[] bookings
.
您可以做这样的事情。请记住,你需要重写预订的toString(),因为这是显示在TextView中。
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));
如果你想保持2-D阵列可以延长 BaseAdapter 一>,以使自定义BookingsAdapter。
If you want to keep the 2-d array you can extend BaseAdapter to make a custom BookingsAdapter.
这篇关于如何加载一个ListView具有多阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!