1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.app; 18 19 import com.example.android.apis.Shakespeare; 20 21 import android.app.Activity; 22 import android.app.ListFragment; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.ArrayAdapter; 27 import android.widget.ListView; 28 29 /** 30 * Demonstration of using ListFragment to show a list of items 31 * from a canned array. 32 */ 33 public class FragmentListArray extends Activity { 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 39 // Create the list fragment and add it as our sole content. 40 if (getFragmentManager().findFragmentById(android.R.id.content) == null) { 41 ArrayListFragment list = new ArrayListFragment(); 42 getFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 43 } 44 } 45 46 public static class ArrayListFragment extends ListFragment { 47 48 @Override 49 public void onActivityCreated(Bundle savedInstanceState) { 50 super.onActivityCreated(savedInstanceState); 51 setListAdapter(new ArrayAdapter<String>(getActivity(), 52 android.R.layout.simple_list_item_1, Shakespeare.TITLES)); 53 } 54 55 @Override 56 public void onListItemClick(ListView l, View v, int position, long id) { 57 Log.i("FragmentList", "Item clicked: " + id); 58 } 59 } 60 } 61