使用ArrayList填充ListView?

时间:2011-02-21 20:20:16

标签: java android listview arraylist

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

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

5 个答案:

答案 0 :(得分:216)

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

这是Android developer guide所说的:

  

管理由任意对象数组支持的ListAdapter的{​​{1}}。默认情况下,此类期望提供的资源ID引用单个ListView。如果要使用更复杂的布局,请使用也带有字段ID的构造函数。该字段ID应引用较大布局资源中的TextView

     

但是引用了TextView,它将填充数组中每个对象的TextView。您可以添加自定义对象的列表或数组。覆盖对象的toString()方法,以确定将为列表中的项目显示的文本。

     

要为数组显示使用toString()以外的内容,例如TextViews,或者除了ImageViews结果之外还有一些数据填充视图,请覆盖toString()返回你想要的视图类型。

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

getView(int, View, ViewGroup)

答案 1 :(得分:13)

tutorial

同时查找ArrayAdapter界面:

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

答案 2 :(得分:7)

尝试使用以下答案使用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");   
    }
}

答案 3 :(得分:2)

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

答案 4 :(得分:0)

以下是如何实现列表视图的示例:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //We have our list view
    final ListView dynamic = findViewById(R.id.dynamic);

    //Create an array of elements
    final ArrayList<String> classes = new ArrayList<>();
    classes.add("Data Structures");
    classes.add("Assembly Language");
    classes.add("Calculus 3");
    classes.add("Switching Systems");
    classes.add("Analysis Tools");

    //Create adapter for ArrayList
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, classes);

    //Insert Adapter into List
    dynamic.setAdapter(adapter);

    //set click functionality for each list item
    dynamic.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.i("User clicked ", classes.get(position));
        }
    });



}