ITeye博客

extends ListActivity

AndLi · 2011-10-02 10:26 · ListView · 移动开发 · Markdown


 

package com.epoint.listview;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Main extends ListActivity {
	/**
	 * 注意本程序没有按通常的写法为Activity加载一个布局文件,
	 * 而是通过setListAdapter(adapter)自动添加一个ListView填充整个ListActivity
	 * 
	 */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("最简单的ListView");
		String[] numbers = new String[] { "Zero", "First", "Scound", "Three",
				"Four", "Five", "Six", "Seven", "Seven", "Seven" };

		/**
		 * 第一个参数:当前的上下文 
		 * 第二个参数:每个列表项的布局文件 
		 * 第三个参数:参数
		 * Tip:你可以使用android.R.layout.simple_list_item_1代替R.layout.list_item,改变ListView显示的样式
		 */

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.list_item, numbers);
		setListAdapter(adapter);// 将adapter放入ListView中
		ListView listview = getListView();
		/**
		 * The setTextFilterEnabled(boolean) method turns on text filtering for
		 * the ListView, so that when the user begins typing, the list will be
		 * filtered.不太明白什么意思?
		 */
		listview.setTextFilterEnabled(true);
		// 设置ListView的监听事件
		listview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String s = ((TextView) view).getText().toString();
				Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT)
						.show();
			}
		});
	}
}

 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"/>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。