Android ListView Example in Java

Android ListView Example in Java: ListView in Android Studio

A ListView in Android is a view that displays a group of several items into a vertical scroll-able List. The elements of the List are inserted automatically into the list with the help of an Adapter. The adapter gets the elements from a source such as a Database or an Array. In this Android ListView Example in Java, we will use arrays to store Data.

 

Android ListView Example in Java
Android ListView Example in Java/Android Studio Listview

 

What is An Adapter

An adapter basically works as a middle component between UI (User Interface) and Data Source (Array, Database, WEB, etc). The Adapter gets data from the data source and then sends it to the Adapter View. Adapter View shows the data on different views on the screen such as ListView, GridView, Spinner, etc.

 

Lets start coding with Our Android ListView Example in Java

 

1- First of all start a new Android Studio Project and name it ListViewAndroid or whatever you would like to name it.

2- Before starting you should know that in this example we are getting our data from Arrays.

3- We make 3 Arrays “country[]” , “continent[]” , “images[]”. Then the name of the countries will be stored in “country[]”. The names of the continents will be stored in “continent[]” and the maps images will be stored in “images[]”.

4- So now copy and paste all the images (images of maps for this example) into your Drawable folder in Android Studio.

5- Right-click on the “layout” folder inside “res”, select “New” then “Layout Resource File” and create a new layout file.

6- Name your new Layout Resource file as “row_listview” OR name it whatever you like.

7- We will handle the look of a single member of our ListView here.

8- We have a Linear Layout as our Main Layout and an ImageView, we have another Linear Layout that has 2 TextViews. ImageView will show images on the UI while TextViews are responsible for showing COUNTRIES and CONTINENT’s names.

 

 row_listview

 

10- Now in your “activity_main” file add a ListView with “match_parent” width and height. Now First, change the default Constraint Layout to Relative Layout.

 

activity_main

 

11- Now come to your “MainActivity.java” file and add ListView and Arrays with your desired values.

 

“MainActivity.java”

12- Now to use the Adapter we will make an Adapter class with the name “MyAdapter” outside our onCreate method.

13- class “MyAdapter” will extend “ArrayAdapter<String>”.

14- We will make a constructor for our class because there is no default constructor available in the parent class.

15- After that, we will override this method:

 

Here we will use LayoutInflater to inflate our custom layout “row_listview” and then hold it in a View type variable view.

16- Provide the ids of the views used in our “row_listview” layout (ImageView and TextViews) and return view ( the View type variable ).

17- In the onCreate method set Adapter on our ListView.

18- Finally the complete “MainActivity.java” file will look like this:

 

OUTPUT

 

 

Android ListView Example in Java
Android ListView Example in Java/Android Studio Listview

 

Android ListView Example in Java
Android Listview

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

People Also Read

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!