ListView Containers Resources https://developer.android.com/guide/topics/ui/layout/listview.html https://developer.android.com/reference/android/widget/listview.html Creating a ListView A ListView is a container that displays a vertical list of other View objects. For example, we can create a ListView that contains a list of TextView objects where the text in each TextView comes from an array of Strings. A ListView is created by adding a ListView tag to the layout file for the activity in which we want to display the list. Be sure to give the ListView a unique id. <ListView android:id="@id/nameslistview" android:layout_width="match_parent" android:layout_height="wrap_content"...
When your ListView has more elements in it than can be displayed on the screen, a scrollbar automatically appears on the right. There is no need to put the ListView in a ScrollView layout! When the layout_height attribute is set to wrap_content, some of the objects in the list may appear behind other controls that are displayed on the screen above or below the ListView.. To avoid this problem you can place the ListView in a RelativeLayout or ConstraintLayout and specify that the ListView is above or below the other widgets in the activity. Populating ListViews with an Adapter In the oncreate() function of the activity that displays the ListView, we populate the ListView with content. To do so, we use an adapter. An adapter is an object that retrieves data from an external data source (e.g. array, ArrayList, Database) and creates one new View object for each element in the data source. Suppose we have an array named names. private String names[] = {"Alice, Bob, Ceci }; There are a number of built-in adapters. An ArrayAdapter is a generic class that takes an array of objects as an argument (the 3 rd argument) and creates a TextView object for each object in the array. To define an ArrayAdapter we call the ArrayAdapter s constructor, specifying the type of objects in the array, as shown below. ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.r.layout.simple_list_item_1, names);
The first argument is the activity that the ListView is in and the second argument is an Android layout provided in the SDK that displays each String in the array in a one line TextView object. Next, we instruct the ListView object to use the adapter by calling the ListView s setadapter() method. ListView listview = (ListView) findviewbyid(r.id.nameslistview); listview.setadapter(adapter); Handling OnClick Events for the TextView Objects in the ListView Each TextView element that is added to the ListView is clickable. To handle these events, after we create the adapter, we need to create a Listener object to listen for onitemclick events and handle them. Below is code that creates an instance of a Listener class. The class has one public method named onitemclicked(). When an item is clicked, onitemclicked() is called, passing into the method is a reference to the TextView object that was clicked. We can use this object to get the string that is in the TextView and, for example, display it in a toast. // Create a message handling object as an anonymous class. AdapterView.OnItemClickListener handler = new AdapterView.OnItemClickListener() { public void onitemclick(adapterview parent, View v, int position, long id) { String name = ((TextView) v).gettext().tostring(); Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}; } Last, we need to register the handler with the ListView. listview.setonitemclicklistener(handler); Displaying a ListView in a Fragment To display a ListView in a fragment you add the above code (with a few subtle changes) to the fragment s oncreateview() method. In the example below, we assume the fragment s layout is named names_list.xml, the ListView has an id named nameslistview, and the activity that displays the fragment is named MyViewPager. The subtle changes include the following: 1. You first need to inflate the fragment s xml file and save a reference to the root View object that the inflate() method returns and return it after you create the adapter. 2. Since oncreateview() is not an activity class you ll need to call getactivity() when you need a reference to the activity or a context. 3. If the names array is a field in the activity that displays the fragment, make the array public and static in the activity. For example, if the array is in a class named MyViewPager, then you can get a reference to the array using MyViewPager.names.
4. To get a reference to the ListView call findviewbyid() on the rootview object. @Override public View oncreateview(layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) { View rootview = inflater.inflate(r.layout.names_list, container, false); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.r.layout.simple_list_item_1, MyViewPager.names); ListView listview = (ListView) rootview.findviewbyid(r.id.nameslistview); listview.setadapter(adapter); AdapterView.OnItemClickListener handler = new AdapterView.OnItemClickListener() { public void onitemclick(adapterview parent, View v, int position, long id) { String name = ((TextView) v).gettext().tostring();
}; } /* Do something with the name */ Toast.makeText(getActivity().getApplicationContext(), name, Toast.LENGTH_SHORT).show(); listview.setonitemclicklistener(handler); } return rootview;