Android ListView - Fill With Array

java array

*This is a simple ListView tutorial.Here is what we do:

What is a ListView?

A ListView is an android widget that allows us to render a list of scrollable items.

ListView is an adapterview like gridview and spinner.

This means that it requires an adapter for it to insert its items. The adapter becomes responsible for pulling data from a content source.

This source can be an array or something more complex like database or from the network.

Not only that but the adapter will also be responsible for converting each item result into a view that will be placed into the listview.

This is because as an adapterview the ListView does not know the details, such as type and contents, of the views it contains.

So it will ask for the views on demand from a ListAdapter as needed. For instance it asks for these views as the user scrolls up or down.

Each of the views in the ListView is positioned immediately below the previous view in the list.

ListView API Definition

Here's ListView's API definition.

   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.AdapterView<android.widget.ListAdapter>
               ↳    android.widget.AbsListView
                   ↳    android.widget.ListView

Clearly you can see ListView is residing in the android.widget package and deriving from the abstract AbsListView class.

What is ArrayAdapter?

ArrayAdapter is concrete class that derives from the BaseAdapter and is backed by an array of arbitrary objects.

ArrayAdapter requires us to pass a single TextView as a resource as the layout. This is for simple layouts where you don't have to create a custom adapter.

For complex layouts you can create your own adapter that derives from this class.

ArrayAdapter can take as the data source as Lists, String Arrays or Arrays of custom objects.

If you use an Array of custom objects then be sure to override the toString() method so as to customize the string that will get displayed in the adapterview.

(a). ArrayAdapter Definition

ArrayAdapter resides in the android.widget package:

package android.widget;

It derives from BaseAdapter:

public class ArrayAdapter extends BaseAdapter..{}

And implements Filterable and ThemedSpinnerAdapter interfaces:

public class ArrayAdapter extends BaseAdapter implements Filterable ThemedSpinnerAdapter{}

Our MainActivity

This is our main activity. This is our launcher activity. This class will be the entry point to our application.

First we define our package then import other packages via the import statements.

Then we make our class derive from AppCompatActivity.

We will maintain an array which will act as our data source:

 String[] spacecrafts={"Juno","Hubble","Casini","WMAP","Spitzer","Pioneer","Columbia","Challenger","Apollo","Curiosity"};

We will pass this array into our arrayadapetr constructor and set its instance to our ListView:

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spacecrafts);

Let's look at the full source code.

package com.tutorials.hp.listviewarray;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

 ListView lv;
 String[] spacecrafts={"Juno","Hubble","Casini","WMAP","Spitzer","Pioneer","Columbia","Challenger","Apollo","Curiosity"};

 protected void onCreate(Bundle savedInstanceState) {

    lv= (ListView) findViewById(;

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spacecrafts);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(MainActivity.this, spacecrafts[i], Toast.LENGTH_SHORT).show();



Our XML Layout

This is our main activity's layout. It is our activity_main.xml file.

We use the following elements:

  1. RelativeLayout - Our ViewGroup.
  2. ListView - Our adapterview.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""


How To Run

  1. Download the project.
  2. You'll get a zipped file,extract it.
  3. Open the Android Studio.
  4. Now close, already open project.
  5. From the Menu bar click on File >New> Import Project.
  6. Now Choose a Destination Folder, from where you want to import project.
  7. Choose an Android Project.
  8. Now Click on “OK“.
  9. Done, your done importing the project,now edit it.

More Resources

Resource Link
GitHub Browse Browse
GitHub Download Link Download


How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post