YouTip LogoYouTip

Android List Fragment

The basic implementation of a List Fragment is used to create a list of items within a fragment. ![Image 1: image](#) * * * ## Example This example explains how to create a List Fragment based on ArrayAdapter. Let's start by following these steps: | Step | Description | | --- | --- | | 1 | Create an Android application using Android Studio, named List Fragment, with the package name cn.uprogrammer.listfragment. | | 2 | Modify the strings file, adding new string constants in res/values/string.xml. | | 3 | Create a layout file named list_fragment.xml under res/layout to define the list fragment, and add the tag in activity_main.xml. | | 4 | Create the MyListFragment.java file, containing onCreateView(), onActivityCreated(), and OnItemClickListener(). | | 5 | Launch the Android emulator to run the application and verify the results of the changes made. | Before starting to code, initialize the string constants in the string.xml file located in the res/values directory. listfragment Settings Hello world! List Fragment Demo imgdesc Sun Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Below is the content of the res/layout/activity_main.xml file, which includes a LinearLayout and a fragment tag. Below is the content of the res/layout/list_fragment.xml file, which includes a LinearLayout, a ListView, and a TextView. Below is the content of the src/cn.uprogrammer.listfragment/MyListFragment.java file. Before starting to code, you need to follow these steps: * Create the MyListFragment class, inheriting from ListFragment. * Inside the onCreateView() method, inflate the view using the list_fragment.xml layout defined above. * Inside the onActivityCreated() method, create an ArrayAdapter using the string array resource R.array.Planets defined in string.xml, set the adapter to the ListView, and set an item click listener for the list. * Inside the onItemClick() method, display the position of the clicked item as a Toast message. package cn.uprogrammer.listfragment;import android.app.ListFragment;import android.annotation.SuppressLint;import android.app.ListFragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.Toast;public class MyListFragment extends ListFragment implements OnItemClickListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.list_fragment, container, false); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Planets, android.R.layout.simple_list_item_1); setListAdapter(adapter); getListView().setOnItemClickListener(this); } @Override public void onItemClick(AdapterView parent, View view, int position,long id) { Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show(); }} Below is the content of MainActivity.java: package cn.uprogrammer.listfragment;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }} Below is the content of the AndroidManifest.xml file: Let's run the List Fragment application we just modified. I assume you have already created an AVD when setting up the environment. Open the activity file in your project and click the ![Image 2: image](#) icon in the toolbar to run the application in Android Studio. Android Studio installs the application on the AVD and launches it. If everything goes well, you will see the following on the emulator window: ![Image 3: image](#)
← Go Continue StatementGo Nested Loops β†’