ListView组件是一个显示组件,继承AdapterView基类,前面已经介绍了分别使用ArrayAdapter,SimpleAdapter,扩展BaseAdapter来为LisView提供列表项[http://blog.csdn.net/tuke_tuke/article/details/50527018](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);
}
};
}
~~~
![](https://box.kancloud.cn/2016-03-10_56e0d9ace787d.jpg)
- 前言
- Eclipse搭建android环境及Genymotion模拟器安装问题解决方法
- 表格布局(TableLayout)及重要属性
- 帧布局(FrameLayout)及属性
- layout_width和width,layout_height和height
- UI组件之TextView及其子类
- UI组件之TextView及其子类(一)TextView和EditText
- UI组件之TextView及其子类(二)RadioButton和CheckBox
- UI组件之TextView及其子类(三)ToggleButton和Switch
- UI组件之TextView及其子类(四)AnalogClock,DigitalClock
- UI组件之TextView及其子类(五)计时器Chronometer
- UI组件之ImageView及其子类(一)ImageView显示图片
- UI组件之ImageView及其子类(二)ImageButton ,ZoomButton
- UI组件之AdapterView及其子类关系,Adapter接口及其实现类关系
- UI组件之AdapterView及其子类(一)三种Adapter适配器填充ListView
- UI组件之AdapterView及其子类(二)GridView网格视图的使用
- UI组件之AdapterView及其子类(三)Spinner控件详解
- UI组件之AdapterView及其子类(四)Gallery画廊控件使用
- UI组件之AdapterView及其子类(五)ListView组件和ListActivity
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
- UI组件之 ProgressBar及其子类(一)ProgressBar进度条的使用
- UI组件之ProgressBar及其子类(二)SeekBar拖动条和RatingBar星级评分条的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost选项卡的 功能和用法
- AlertDialog创建6种对话框的用法
- Android基于监听的事件处理机制
- Android基于回调的事件处理
- Handler消息传递机制(一)
- Handler消息传递机制(二)Handler,Loop,Message,MessageQueue的工作原理
- 启动Activity的两种方式startActivity和startActivityForResult(一)
- 启动Activity的两种方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之间交换数据
- 通过 Intent 传递类对象
- Intent对象详解(一)
- Intent对象详解(二)
- 使用指定的Action,Category调用系统Activity
- 使用Action,Data属性启动系统Activity
- Android数据存储的三种方式-SharedPrefrences,File,SQLite