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

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //无需使用布局文件,-相当于它的布局文件里仅仅有一个ListView
String[] 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)
*/
@Override
protected void onRestoreInstanceState(Bundle state) {
ensureList();
super.onRestoreInstanceState(state);
} /**
* @see Activity#onDestroy()
*/
@Override
protected void onDestroy() {
mHandler.removeCallbacks(mRequestFocus);
super.onDestroy();
} /**
* Updates the screen state (current list and other views) when the
* content changes.
*
* @see Activity#onContentChanged()
*/
@Override
public 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的更多相关文章

  1. Android用户界面UI组件--AdapterView及其子类(五) Spinner和SpinnerAdapter

    Spinner就是下拉框组件,可以自定义下拉布局样式,可以使用ArrayAdapter以及SpinnerAdapter适配 在Adapter中实现SpinnerAdapter,继承BaseAdapte ...

  2. 第3组UI组件:AdapterView及其子类

    1 AdapterView类简介 1.1 AdapterView组件是一组重要的组件,AdapterView本身是一个抽线类,实际使用更多的都是Adapter相关子类,AdapterView具有如下特 ...

  3. UI组件之AdapterView及其子类关系,Adapter接口及事实上现类关系

    AdapterView本身是一个抽象基类,它派生的的子类在使用方法上十分类似.AdapterView直接派生的三个子类:AbsListView.AbsSpinner,AdapterViewAnimat ...

  4. 第四组UI组件:AdapterView及子类

    AdapterView组件是一组重要的组件,AdapterView本省是一个抽象基类,它派生的子类在用法上十分相似,只是显示界面与一定的区别,因此这次针对它们的共性集中讲解,并突出介绍他们的区别. A ...

  5. UI组件之AdapterView及其子类(四)Gallery画廊控件使用

    听说 Gallery如今已经不使用了,API使用ViewPaper取代了,以后再学专研ViewPaper吧如今说说Gallery画廊,就是不停显示图片的意思 Gallery是用来水平滚动的显示一系列项 ...

  6. Android开发 ---基本UI组件6 :只定义一个listView组件,然后通过BaseAdapter适配器根据数据的多少自行添加多个ListView显示数据

    效果图: 1.activity_main.xml 描述: 定义了一个按钮 <?xml version="1.0" encoding="utf-8"?> ...

  7. Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解

    ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...

  8. UI组件之AdapterView及其子类(三)Spinner控件具体解释

    Spinner提供了从一个数据集合中高速选择一项值的办法. 默认情况下Spinner显示的是当前选择的值.点击Spinner会弹出一个包括全部可选值的dropdown菜单或者一个dialog对话框,从 ...

  9. AdapterView及其子类之二:使用ListActivity及ArrayAdapter创建列表

    见归档项目ListActivityDemo.zip. 基本步骤如下: 1.创建一个TextView,用于指定每一个ListView的格式 <?xml version="1.0" ...

随机推荐

  1. Qt5.9 WebChannel

    Qt WebChannel enables peer-to-peer communication between a server (QML/C++ application) and a client ...

  2. guice基本使用,常用的绑定方式(四)

    guice在moudle中提供了良好的绑定方法. 它提供了普通的绑定,自定义注解绑定,按名称绑定等. 下面直接看代码: package com.ming.user.test; import com.g ...

  3. MVP演化论

    本文是翻译MVP: Model-View-Presenter The Taligent Programming Model for C++ and Java(Mike Potel)文章的摘要.该文介绍 ...

  4. (转载)tnsping不是内部或外部命令

    手动添加 D:\app\Administrator\product\11.2.0\client_1\bin 到系统环境变量 path里面

  5. Win10 BackgroundTask

    1.这里面详细的说明了后台任务的搭建 调用等 提示: 1.BackgroundTaskRegistration 里面有这两个事件 OnCompleted/Progress 主要用来UpdateUI 这 ...

  6. 应运而生! 双11当天处理数据5PB—HiStore助力打造全球最大列存储数据库

    阿里巴巴电商业务中历史数据存储与查询相关业务, 大量采用基于列存储技术的HiStore数据库,双11当天HiStore引擎处理数据记录超过6万亿条.原始存储数据量超过5PB.从单日数据处理量上看,该系 ...

  7. luogu P2634 [国家集训队]聪聪可可 点分治

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  8. SQL SEVER (ROLLUP与CUBE,ROW_NUMBER())使用方法

    1.建立测试专用数据: if object_id('TESTDB') is not null drop table TESTDB ), B INT) insert into TESTDB union ...

  9. python与图灵机器人交互(WXPY版本)

    开发者账号:wujunfeng , 开发者key:官网申请  #!/usr/bin/env python#-*- coding:utf-8 -*- @Author : wujf @Time:2018/ ...

  10. Python笔记12-----画图Matplotlib

    1.matplotlib:pyplot和pylab 如: import pylab as pl pl.figure(figsize=(8,6),dpi=100)[建立的图像大小和图的精度] pl.pl ...