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 17package com.example.android.supportv4.app; 18 19import android.os.Bundle; 20import android.util.Log; 21import android.view.View; 22import android.widget.ArrayAdapter; 23import android.widget.ListView; 24 25import androidx.fragment.app.FragmentActivity; 26import androidx.fragment.app.ListFragment; 27 28import com.example.android.supportv4.Shakespeare; 29 30/** 31 * Demonstration of using ListFragment to show a list of items 32 * from a canned array. 33 */ 34public class FragmentListArraySupport extends FragmentActivity { 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 40 // Create the list fragment and add it as our sole content. 41 if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) { 42 ArrayListFragment list = new ArrayListFragment(); 43 getSupportFragmentManager().beginTransaction().add(android.R.id.content, list).commit(); 44 } 45 } 46 47 public static class ArrayListFragment extends ListFragment { 48 49 @Override 50 public void onActivityCreated(Bundle savedInstanceState) { 51 super.onActivityCreated(savedInstanceState); 52 setListAdapter(new ArrayAdapter<String>(getActivity(), 53 android.R.layout.simple_list_item_1, Shakespeare.TITLES)); 54 } 55 56 @Override 57 public void onListItemClick(ListView l, View v, int position, long id) { 58 Log.i("FragmentList", "Item clicked: " + id); 59 } 60 } 61} 62