使用ArrayList填充ListView?

kAn*_*NaN 105 java android listview arraylist

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

我这样做很麻烦.有人可以帮我提供代码吗?

Amo*_*tir 224

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

这是Android开发人员指南所说的:

ListAdapter管理ListView由任意对象数组支持的A.默认情况下,此类期望提供的资源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); 
    }
}
Run Code Online (Sandbox Code Playgroud)


San*_*uja 13

教程

另请查看ArrayAdapter接口:

ArrayAdapter(Context context, int textViewResourceId, List<T> objects)
Run Code Online (Sandbox Code Playgroud)


Kar*_*KPN 10

尝试使用以下答案使用ArrayList填充listview

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");   
    }
}
Run Code Online (Sandbox Code Playgroud)