刚学习android开发,学到ListView的ListView需要适配器来完成根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。在学习中有这样的一个问题ArrayAdapter和SimpleCursorAdapter可以通过SetContentView就可以完成不需要类继承ListActivity
但是SimpleAdapter就需要继承ListActivity实现。想问下这个是不是必须的,大虾讲下原理。

解决方案 »

  1.   

    不是必须的,我个人喜欢直接继承Activity
      

  2.   

    SimpleAdapter不用继承ListActivity,我也是新手,说不清为什么,反正我用的SimpleAdapter都没继承ListActivity
      

  3.   

    继承自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 {@link android.R.id#list} if it's in code)继承自ListActivity的类必须在layout中有一个id为@android:id/list的ListView控件,而且由于java是单继承的,所以继承了ListActivity就不可以再继承其他的类.再从源码角度看一下ListActivity,大概只有两百行代码,唯一有实质性作用的方法就是覆盖了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;
        }
    表明你可以用一个id为empty的ListView表示当前没有元素的显示.再来看看ListActivity用神马xml布局的:<ListView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/list"
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"
     android:drawSelectorOnTop="false"
     />
    坑爹不?所以还是自己继承自Activity算了,ListActivity并没有给太多的好处,反而限制多多