刚学习android开发,学到ListView的
ListView需要适配器来完成根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。在学习中有这样的一个问题ArrayAdapter和SimpleCursorAdapter可以通过SetContentView就可以完成不需要类继承ListActivity
但是SimpleAdapter就需要继承ListActivity实现。
想问下这个是不是必须的,大虾讲下原理。
------解决方案--------------------
继承自ListActivity没什么太大的好处,见sdk doc文档描述:
ListActivity has a default layout that consists of a single, full-screen list
in the center of the screen. However, if you desire, you can customize the
screen layout by setting your own view layout with setContentView() in
onCreate(). To do this, your own view MUST contain a ListView object with the
id "@android:id/list" (or [email protected] android.R.id#list} if it's in code)
[email protected]id:id/list的ListView控件,而且由于java是单继承的,所以继承了ListActivity就不可以再继承其他的类.
再从源码角度看一下ListActivity,大概只有两百行代码,唯一有实质性作用的方法就是覆盖了Activity的onContentChanged方法:
- Java code
@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; }