标签:
package com.example.listview_6; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { private ListView listView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView1 = (ListView) findViewById(R.id.listView1); List<String> list = new ArrayList<String>(); list.add("aa");list.add("bb");list.add("cc");list.add("dd");list.add("ee");list.add("ff"); String[] arr = { "aa", "bb", "cc" ,"dd","ee","ff","gg","hh","ii","jj"};// //正确 //String[] arr2 = new String[5];// 正确 //定义维数和数组初始化不能同时进行 // String[] arr4 = new String{"aa","bb","cc"};////错误 // String[] arr3 = new String[5]{"aa","bb","cc"};//错误///如果提供了数组初始化操作,则不能定义维表达式 // ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.item, objects)//错误 ////只有一个主布局,不需要条目布局,simple_expandable_list_item_1是系统自带的布局 // //第一种方法: // ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1, arr); ///第二种方法 ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); //最后一个参数是List或String[]均可 listView1.setAdapter(adapter); } }如果ListView一行是TextView,可以采用ArrayAdapter来实现,不用创建BaseAdapter
标签:
原文地址:http://blog.csdn.net/u010002184/article/details/51332358