UI组件之AdapterView及其子类(五)ListView组件和ListActivity
ListView组件是一个显示组件,继承AdapterView基类,前面已经介绍了分别使用ArrayAdapter,SimpleAdapter,扩展BaseAdapter来为LisView提供列表项http://blog.csdn.net/tuke_tuke/article/details/50527018,在其中都要在xml文件中定义ListView组件,然后再Activity.java文件中通过findViewById获取组件设置定义好的adapter即可。
但是ListActivity是直接继承Activity的,在ListActivity的源码中已经定义了一个ListView组件属性,ListActivity的子类无需调用setContentView()来显示某个界面,ListActivity的效果就是整个Activity就是一个列表,没有其他的组件。在使用ListActivity时,只需要继承ListActivity,直接传入一个内容Adapter。ListActivity就是一个列表.
MainActivity.java
<span style="font-size:24px;">public class MainActivity extends ListActivity {//继承ListActivity@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//无需使用布局文件,-相当于它的布局文件中只有一个ListViewString[] s={"孙悟空","猪八戒","唐僧"};//创建ArrayAdapter对象,这里使用的是android提供的布局文件//ArrayAdapter<String> ad=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);//下面是使用自定义的一个列表项布局ArrayAdapter<String> ad=new ArrayAdapter<String>(this,R.layout.array_item,s);//设置适配器setListAdapter(ad);}</span>
总结一下:
1,继承ListActivity
2,定义适当的Adapter
3,调用setListAdapter方法设置Adapter
ListActivity的部分源码:
public class ListActivity extends Activity {/*** This field should be made private, so it is hidden from the SDK.* {@hide}*/protected ListAdapter mAdapter;/*** This field should be made private, so it is hidden from the SDK.* {@hide}*/protected ListView mList;private Handler mHandler = new Handler();private boolean mFinishedStart = false;private Runnable mRequestFocus = new Runnable() {public void run() {mList.focusableViewAvailable(mList);}};/*** This method will be called when an item in the list is selected.* Subclasses should override. Subclasses can call* getListView().getItemAtPosition(position) if they need to access the* data associated with the selected item.** @param l The ListView where the click happened* @param v The view that was clicked within the ListView* @param position The position of the view in the list* @param id The row id of the item that was clicked*/protected void onListItemClick(ListView l, View v, int position, long id) {}/*** Ensures the list view has been created before Activity restores all* of the view states.**@see Activity#onRestoreInstanceState(Bundle)*/@Overrideprotected void onRestoreInstanceState(Bundle state) {ensureList();super.onRestoreInstanceState(state);}/*** @see Activity#onDestroy()*/@Overrideprotected void onDestroy() {mHandler.removeCallbacks(mRequestFocus);super.onDestroy();}/*** Updates the screen state (current list and other views) when the* content changes.** @see Activity#onContentChanged()*/@Overridepublic void onContentChanged() {super.onContentChanged();View emptyView = findViewById(com.android.internal.R.id.empty);mList = (ListView)findViewById(com.android.internal.R.id.list);if (mList == null) {throw new RuntimeException("Your content must have a ListView whose id attribute is " +"'android.R.id.list'");}if (emptyView != null) {mList.setEmptyView(emptyView);}mList.setOnItemClickListener(mOnClickListener);if (mFinishedStart) {setListAdapter(mAdapter);}mHandler.post(mRequestFocus);mFinishedStart = true;}/*** Provide the cursor for the list view.*/public void setListAdapter(ListAdapter adapter) {synchronized (this) {ensureList();mAdapter = adapter;mList.setAdapter(adapter);}}/*** Set the currently selected list item to the specified* position with the adapter's data** @param position*/public void setSelection(int position) {mList.setSelection(position);}/*** Get the position of the currently selected list item.*/public int getSelectedItemPosition() {return mList.getSelectedItemPosition();}/*** Get the cursor row ID of the currently selected list item.*/public long getSelectedItemId() {return mList.getSelectedItemId();}/*** Get the activity's list view widget.*/public ListView getListView() {ensureList();return mList;}/*** Get the ListAdapter associated with this activity's ListView.*/public ListAdapter getListAdapter() {return mAdapter;}private void ensureList() {if (mList != null) {return;}setContentView(com.android.internal.R.layout.list_content_simple);}private AdapterView.OnItemClickListener mOnClickListener = new AdapterView.OnItemClickListener() {public void onItemClick(AdapterView<?> parent, View v, int position, long id){onListItemClick((ListView)parent, v, position, id);}};
}
UI组件之AdapterView及其子类(五)ListView组件和ListActivity相关推荐
- UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView
AdapterView的内容一般是包含多项相同格式资源的列表,常用的有5种AdapterView的子类: (1)ListView:简单的列表 (2)Spinner:下拉列表,给用户提供选择 (3)Ga ...
- UI组件之AdapterView及其子类关系,Adapter接口及其实现类关系
AdapterView本身是一个抽象基类,它派生的的子类在用法上十分相似.AdapterView直接派生的三个子类:AbsListView,AbsSpinner,AdapterViewAnimator ...
- UI组件之AdapterView及其子类(四)Gallery画廊控件使用
听说 Gallery现在已经不使用了,API使用ViewPaper代替了,以后再学专研ViewPaper吧现在说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
ExpandableListView是ListView的子类,他在ListView上进行了扩展,它把列表项分成了几组,每组里包含了多个列表项 ExpandableListView的列表项是由Expan ...
- UI组件之AdapterView及其子类(二)GridView网格视图的使用
GridView网格视图属性: android:numColumns="auto_fit" --------列数设置为自动,可以为确定的数值 android:columnWidth ...
- UI组件之AdapterView及其子类(三)Spinner控件详解
Spinner提供了从一个数据集合中快速选择一项值的办法.默认情况下Spinner显示的是当前选择的值,点击Spinner会弹出一个包含所有可选值的dropdown菜单或者一个dialog对话框,从该 ...
- AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表
见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...
- android adapter 组件,Android UI - AdapterView 及其子类
AdapterView AdapterView 是一个抽象类,其派生的子类在用法上十分相似: AdapterView 继承了 ViewGroup: AdapterView 及其子类的继承关系如下: A ...
- 2.5 UI组件-AdapterView及子类(疯狂android学习笔记)
列表视图(ListView)和ListActivity ①直接使用ListView创建 ②让Activity继承ListActivity(相当于该activity显示的组件为ListView) 提示: ...
最新文章
- linux 支持7代cpu型号,win7最高支持几代cpu
- BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点
- mysql常用命令--入门
- android多屏幕适配资源生成,android – 多屏幕适配相关
- 因为有了这个画图工具集,老师同学都说我画的图有特色(Processon,draw.io,Xmind)
- 算法竞赛入门经典(第二版) | 例题4-5 追踪电子表格中的单元格 (UVa512,Spreadsheet Tracking,World Finals)(解法二)
- catia中sew的用法_CATIA超级副本(PowerCopy)使用方法总结 | 坐倚北风
- iPhone 软件:xlate free 编码的好帮手!
- linux pap认证,配置PPP PAP 认证
- sql优化——模糊查询
- JavaScript-闭包closure
- 三言两语聊Python模块–文档测试模块doctest
- linux光纤盘刷新,Linux 在shell终端中清空DNS缓存,刷新DNS的方法(ubuntu,debian)
- 为什么蓝鸽的听力下载完还是听不了_听力训练方法干货-说说我与雅思听力的那些事情...
- 解决win8 64位版本下无法使用debug
- ASP站内搜索代码#
- OpenBot开源小车
- 查看笔记本当前链接Wifi的密码
- avdd-supply and vdd_io-supply两个属性解析调用regulator_get(dev, “vdd_io“)
- Unity3d学习笔记 var 关键字
热门文章
- Errors occurred during the build. Errors running builder 'DeploymentBuilder' on project 'drp2.8'. ja
- 初等数论--整除--整数表示:算数分解定理/素因数分解式/进制表示
- 【django】配置数据库(mysql)
- armv8-M(cortex-m) Trustzone总结和介绍
- WIN32 使用 MUTEX 实现禁止多开
- c++11中智能指针的原理,使用,实现
- crackme 逆向寒假生涯(22/100)
- 图片轮播器,relativelayout,外加textview小结
- 2020-11-11(对话框简单总结)
- python实现AES算法