Simple ListView Using ArrayAdapter in Android

In android, An adapter is a bridge between UI component and data source that causes us to fill information in UI component. It holds the information and send the information to Adapter see at that point view can takes the information from the Adapter view and demonstrates the information on various perspectives like listview, gridview, spinner etc. Array Adapter is more simple and commonly used Adapter in android.

At whatever point you have a list of single type of items which is backed by an array, you can utilize ArrayAdapter. For example, list of telephone contacts, countries or names.

As a matter of course, ArrayAdapter expects a Layout with a single TextView, If you need to utilize more intricate perspectives implies more customization in grid items or list items, please avoid ArrayAdapter and utilize custom adapters.

Parameters of ArrayAdapter :

The first parameter is used to pass the context means the reference of current class. Here this is a keyword used to show the current class reference. We can also use getApplicationContext(), getActivity() in the place of this keyword.

The second parameter is resource id used to set the layout(xml file) for list items in which you have a text view.

  • textViewResourceId :

The third parameter is textViewResourceId which is used to set the id of TextView where you want to display the actual text.

The fourth parameter is an array of objects, used to set the array of elements in the textView. We can set the object of array or array list here.

1.  Create a new project and name it ArrayAdapterExample.

  • Open Android Studio -> Select File -> New -> New Project. Fill the forms and click “Finish” button.

2.  Now open app -> res -> layout -> activity_main.xml and add following code :

Activity_Main.Xml

3.  Now Open app -> java -> package -> MainActivity.java and add the below code. Here we will use ArrayAdapter to display the items in Listview.

Main_Activity.java

Output :-

Now run the App in Emulator and you will see the below output:

Listview-using-arrayadapter

 

About Jaimin Patel

I am a Web Developer. I have been blogging and researching since 1 years and like to share my knowledge and research with the world. Peace..!!

View all posts by Jaimin Patel →

Leave a Reply

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