Android编程权威指南-第十六章挑战练习

在CrimeListFragment.java中添加OnCreateView方法

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View v = inflater.inflate(R.layout.fragment_crime_list, parent, false);
        ListView listView = (ListView)v.findViewById(android.R.id.list);//安卓内置的id
        listView.setEmptyView(v.findViewById(android.R.id.empty));//安卓内置id
        mAddButton = (Button)v.findViewById(R.id.add_button);
        mAddButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Crime crime = new Crime();
                CrimeLab.get(getActivity()).addCrime(crime);
                Intent i = new Intent(getActivity(), CrimePagerActivity.class);
                i.putExtra(CrimeFragment.EXTRA_CRIME_ID, crime.getId());
                startActivityForResult(i, 0);
            }
        });
        return v;
    }

新建fragment_crime_list.xml文件


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView 
        android:id="@android:id/list"//内置id
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <LinearLayout 
        android:id="@android:id/empty"//内置id
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/empty"/>
        <Button 
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD"/>
    LinearLayout>

FrameLayout>

你可能感兴趣的:(练习)