你需要通过进行操作
ArrayAdapter,以使ArrayList(或任何其他集合)适应布局中的项目(ListView,Spinner等)。
这是Android开发人员指南所说的:
一个
ListAdapter管理
ListView任意对象数组支持的。默认情况下,此类期望提供的资源ID引用单个
TextView。如果要使用更复杂的布局,请使用也带有字段ID的构造函数。该字段ID应该
TextView在较大的布局资源中引用a 。
然而,
TextView被引用,将填充有
toString()阵列中的每个对象的。你可以添加自定义对象的列表或数组。重写
toString()对象的方法,以确定将为列表中的项目显示什么文本。
要使用
TextViews数组显示以外的其他功能(例如)
ImageViews,或者要在
toString()结果中填充一些数据,请覆盖
getView(int, View, ViewGroup)以返回所需的视图类型。
因此,你的代码应如下所示:
public class YourActivity extends Activity { private ListView lv; public void onCreate(Bundle saveInstanceState) { setContentView(R.layout.your_layout); lv = (ListView) findViewById(R.id.your_list_view_id); // Instanciating an array list (you don't need to do this, // you already have yours). List<String> your_array_list = new ArrayList<String>(); your_array_list.add("foo"); your_array_list.add("bar"); // This is the array adapter, it takes the context of the activity as a // first parameter, the type of list view as a second parameter and your // array as a third parameter. ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, your_array_list ); lv.setAdapter(arrayAdapter); }}


