使用ArrayList填充ListView?

我的Android应用程序需要使用ArrayList的数据填充ListView

我做这个很麻烦 有人可以帮助我的代码?

你需要通过一个ArrayAdapter来实现,它将使你的ArrayList(或其他任何集合)适应你的布局(ListView,Spinner等)中的项目。

这就是Android开发者指南所说的:

pipe理由任意对象数组支持的ListView ListAdapter 。 默认情况下,这个类需要提供的资源id引用一个TextView 。 如果您想使用更复杂的布局,请使用也需要字段ID的构造函数。 该字段ID应该引用较大的布局资源中的TextView

然而, TextView被引用,它将被填充数组中每个对象的toString() 。 您可以添加自定义对象的列表或数组。 重写对象的toString()方法,以确定列表中的项目将显示哪些文本。

要使用TextViews以外的其他数据显示(例如ImageViews )或除了toString()结果之外还有一些数据填充视图,请覆盖getView(int, View, ViewGroup)以返回所需的视图types。

所以你的代码应该是这样的:

 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); } } 

一步一步的教程

另外查看ArrayAdapter接口:

 ArrayAdapter(Context context, int textViewResourceId, List<T> objects) 

尝试下面的答案来填充使用ArrayList的列表视图

 public class ExampleActivity extends Activity { ArrayList<String> movies; public void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); setContentView(R.layout.list); // Get the reference of movies ListView moviesList=(ListView)findViewById(R.id.listview); movies = new ArrayList<String>(); getMovies(); // Create The Adapter with passing ArrayList as 3rd parameter ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, movies); // Set The Adapter moviesList.setAdapter(arrayAdapter); // register onClickListener to handle click events on each item moviesList.setOnItemClickListener(new OnItemClickListener() { // argument position gives the index of item which is clicked public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3) { String selectedmovie=movies.get(position); Toast.makeText(getApplicationContext(), "Movie Selected : "+selectedmovie, Toast.LENGTH_LONG).show(); } }); } void getmovies() { movies.add("X-Men"); movies.add("IRONMAN"); movies.add("SPIDY"); movies.add("NARNIA"); movies.add("LIONKING"); movies.add("AVENGERS"); } } 
 public class Example extends Activity { private ListView lv; ArrayList<String> arrlist=new ArrayList<String>(); //let me assume that you are putting the values in this arraylist //Now convert your arraylist to array //You will get an exmaple here //http://www.java-tips.org/java-se-tips/java.lang/how-to-convert-an-arraylist-into-an-array.html private String arr[]=convert(arrlist); @Override public void onCreate(Bundle bun) { super.onCreate(bun); setContentView(R.layout.main); lv=(ListView)findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , arr)); } }