如何使用多个数组加载ListView

时间:2011-03-01 17:14:41

标签: android android-layout android-listview

我正在尝试从以下数组中加载我的ListView

static final String[][] Bookings = new String[][] {{"WC11", "John Smith"},{"WX11", "Terry Jones"}};

我尝试使用以下代码,但Eclipse不会接受它。

ListView bkgList = (ListView) findViewById(R.id.bkg_list);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, Bookings));

它表示'构造函数ArrayAdapter(ListVActivity,int,String [] [])未定义'。如何将我的数组加载到ListView中?

2 个答案:

答案 0 :(得分:2)

ArrayAdapter用于1-d数组,构造函数需要类似String[] bookings的内容。

你可以这样做。请记住,您需要覆盖Booking的toString(),因为这是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));

如果您想保留二维数组,可以扩展BaseAdapter以制作自定义BookingsAdapter。

答案 1 :(得分:0)

如果您想要TextView的每一行ListView多个BaseAdapter,您可能需要考虑继承ArrayAdapter而不是使用{{1}}。

有关如何执行此操作的示例,请参阅here