我研究了API演示,他们有一个画廊示例,其中仅显示文本,代码仅使用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 });
Run Code Online (Sandbox Code Playgroud)
如果您只想显示静态的String对象列表,则ArrayAdapter应该执行以下操作:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new String[] {People.NAME});
Run Code Online (Sandbox Code Playgroud)
但是,如果希望以后可以将更多String对象添加到列表中,则应使用List。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_gallery_item, new ArrayList<String>());
Run Code Online (Sandbox Code Playgroud)