ArrayAdapter是一个简单的适配器,他的作用是将一个数组中的内容放入listView中。listView的item必须为textView。
MainActivity.java
1 package com.kale.listview; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.widget.ArrayAdapter; 6 import android.widget.ListView; 7 8 public class MainActivity extends Activity { 9 10 ListView redLv;11 @Override12 protected void onCreate(Bundle savedInstanceState) {13 super.onCreate(savedInstanceState);14 setContentView(R.layout.activity_main);15 16 redLv = (ListView)findViewById(R.id.red_listView_id);17 18 String []arr01 = {"01","02","03","04","05","06"};19 //将数组送入arrayAdapter,这里的item对象必须是一个textView。20 //系统已经默认将数组中的信息放入textView了,所以无需指定额外的id21 ArrayAdapteradapter01 = new ArrayAdapter (this, R.layout.list_item,arr01);22 //设置adapter23 redLv.setAdapter(adapter01);24 }25 }
xml
16 7 8 16 17 18
listview_item.xml