使用ArrayList填充ListView?


109

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

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

Answers:


228

您需要通过进行操作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); 
    }
}

1
// @ Amokrane Chentir:我无法让你...你能输入完整程序
吗?。

1
@KanNaN:当然,您能看看我的编辑内容对您来说是否更明确?
Amokrane Chentir 2011年

1
@ Amokrane Chentir:我认为必须在“ findViewById”之前调用“ setContentView”方法,否则将找不到ListView。
Petru 2012年

无法识别List <String>。为什么?我正在使用android studio。
Ali.Rashidi '16


11

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

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

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



}

如果您使用的是Android Studio,它将自动填充onItemClick()方法。现在,您所要做的只是向其中添加功能。就我而言,我只是记录了列表中的每个元素。
Syed Sadman
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.