listView 简括一

listView用于垂直滚动显示文本

 

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

 

 public class HelloListView extends ListActivity {

	static final String[] COUNTRIES = new String[] {
	    "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
	    "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
	    "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
	    "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
	    "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia"
	    };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));//context ,一个必须包含一个textview控件的布局文件,数组
		//把数组中的数据填充到layout文件为list_item的xml文件中		
		ListView lv = getListView();//找到listview
		lv.setTextFilterEnabled(true);//是否启用过滤,这里没有作用
      lv.setOnItemClickListener(listener); // 点击一个item的事件		
	}
  private OnItemClickListener listener=new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		          Toast.LENGTH_SHORT).show();
		//(TextView) view)  因为list_item中是用textview显示,所以强制转换
	}
};
}

 

这样把绑定的数据(就是COUNTRIES这个数组)写到activity很凌乱,可以写到res/values/String   文件中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, ListViewActivity!</string>
    <string name="app_name">ListView</string>        
    <string-array name="countries_array">
        <item>Bahrain</item>
        <item>Bangladesh</item>
        <item>Barbados</item>
        <item>Belarus</item>
        <item>Belgium</item>
        <item>Belize</item>
        <item>Benin</item>
    </string-array>
</resources>
 

   public class HelloListView extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		String[] contriesStrings=getResources().getStringArray(R.array.countries_array);//得到res/value/string 文件中定义的name为countries_array的数组

		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, contriesStrings));//context ,一个必须包含一个textview控件的布局文件,数组
		//把数组中的数据填充到layout文件为list_item的xml文件中		
		ListView lv = getListView();//找到listview
		lv.setTextFilterEnabled(true);
      lv.setOnItemClickListener(listener); // 点击一个item的事件		
	}
  private OnItemClickListener listener=new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		          Toast.LENGTH_SHORT).show();
		//(TextView) view)  因为list_item中是用textview显示,所以强制转换
	}
};
}
 

 listView 简括一listView 简括一

 

 

你可能感兴趣的:(ListView)