我看了APIDemos,他们有一个画廊的例子,他们只显示文本,但代码只是使用Cursor,但我想使用String数组.我该怎么做呢?这是API演示中的示例代码:
Cursor c = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);
SpinnerAdapter adapter = new SimpleCursorAdapter(this,
// Use a template that displays a text view
android.R.layout.simple_gallery_item,
// Give the cursor to the list adatper
c,
// Map the NAME column in the people database to...
new String[] {People.NAME},
// The "text1" view defined in the XML template
new int[] { android.R.id.text1 });
解决方法:
如果您只想显示String对象的静态列表,则ArrayAdapter
应该:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});
但是,如果您希望以后能够向列表中添加更多String对象,则应使用List.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());