package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class ListViewTest extends Activity {
/** Called when the activity is first created. */
ListView listview = null;
ArrayAdapter adapter = null;
ArrayList<String> list1 = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView)findViewById(R.id.listview);
list1.add("pfg");
list1.add("124");
list1.add("234");
list1.add("456");
list1.add("567");
adapter = new ArrayAdapter(this,R.layout.view,R.id.textview1,list1); //這裡使用的是自訂的layout和TextView控制項,注意使用自定的layout時需要用對ArrayAdapter的建構函式(ArrayAdapter(Context
context, int resource, int textViewResourceId,
List<T> objects) ),其實也不難理解因為要指定int textViewResourceId就需要知道是在哪個layout中用int resource來表示。
//adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list1); //這裡使用的android內建的android.R.layout.simple_list_item_1
listview.setAdapter(adapter);
}
}
main.xml和view.xml文檔見(http://blog.csdn.net/pfgmylove/article/details/6688094)