Android Listview in Java with Example

18 Feb, 2021
A ListView is a type of AdapterView that displays a vertical list of scroll-able views and each view is placed one below the other. Using adapter, items are inserted into the list from an array or database. For displaying the items in the list method setAdaptor() is used. setAdaptor() method conjoins an adapter with the list.
Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list.
The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result. So, it is the main source to pull data from strings.xml file which contains all the required strings in Java or XML files.
Now let’s understand how to use a listview in an android application with an example. In the example, let’s create an android application that will display a list of tutorials available in GeeksforGeeks portal.
Step 1: Create a new project
Click on File, then New => New Project.Choose “Empty Activity” for the project template.Select language as Java.Select the minimum SDK as per your need.
Click on File, then New => New Project.
Choose “Empty Activity” for the project template.
Select language as Java.
Select the minimum SDK as per your need.
Step 2: Modify activity_main.xml fileAdd a ListView in the activity_main.xml file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <ListView        android:id="@+id/list"        android:layout_width="match_parent"        android:layout_height="match_parent"/></LinearLayout>
Step 3: Modify MainActivity.java fileIn this section, let’s design the backend of the application. Go to MainActivity.java. Now in the java file create a string array and store the values you want to display in the list. Also, create an object of ListView class. In onCreate() method find Listview by id using findViewById() method. Create an object of ArrayAdapter using a new keyword followed by a constructor call. The ArrayAdaptor public constructor description is below:
public ArrayAdapter (Context context, int Resource, T[ ] objects)
Description
According to this pass the argument in ArrayAdapter Constructor and create an object. At last, conjoin the adapter with the list using setAdapter() method.
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;  public class MainActivity extends AppCompatActivity {      ListView l;    String tutorials[]        = { "Algorithms", "Data Structures",            "Languages", "Interview Corner",            "GATE", "ISRO CS",            "UGC NET CS", "CS Subjects",            "Web Technologies" };      @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        l = findViewById(R.id.list);        ArrayAdapter<String> arr;        arr            = new ArrayAdapter<String>(                this,                R.layout.support_simple_spinner_dropdown_item,                tutorials);        l.setAdapter(arr);    }}
Output
android
Android-View
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android RecyclerView in Kotlin
Broadcast Receiver in Android With Example
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java