ArrayAdapter in Android
Hello there, this time I am going to share some useful information about ArrayAdapter in Android, I hope it will help you in Android development…
When talking about adapters, it appears that the adapter acts as a data transfer medium between the UI component and the data source. It transforms data from data sources into view elements that may be shown in the user interface component. All the types of adapters are classes. ArrayAdapter is one of the most used adapters in Android. This adapter may be used to create views for an AdapterView, and it returns a view for each object in a collection of data objects you give. It can also be used with list-based user interface widgets like ListView, GridView, StackView and Spinner. You may use ArrayAdapter when you have a list of single type items that are stored in an array.
To create ArrayAdapter we must supply a single TextView as a layout resource to ArrayAdapter. This is for basic layouts where a special adapter isn’t required. Lists, String Arrays, and Arrays containing custom objects can all be used as data sources for ArrayAdapter. You can use CustomArrayAdapter instead of ArrayAdapter if you want a more complicated layout.
Let’s implement a simple project for further understanding …
First we need to create a Layout Resource File with a ListView. Below I will present a simple layout resource file as an example,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
</RelativeLayout>
To make it easier for you to understand, I used the Default String ArrayAdapter for this project.
MainActivity.java
package com.chathu.arrayadapterexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Specify ListView Id within the parameter ListView listView = findViewById(R.id.listView);
//Specify list items within array (below we are defining items inside String Array) String items[] = new String[]{"Adam", "Andrew", "Berry", "Carl","Lara"};
//Next we specify ArrayAdapter (We use String ArrayAdapter in this code) ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, items);
//connect adapter listView.setAdapter(arrayAdapter);
}
}
In here I specified my list items within the Array and next I specified the ArrayAdapter in code. Finally, I connected ListView and adapter.
I used simple_list_item_1 to change the way ListView looks. There are other such default layouts provided by Android. For a example simple_list_item_2 is another default layout that provided by Android.
And you can also see our final ListView output below,
So, I hope this article was helpful for you to get a basic idea about ArrayAdapter in Android…🙌
Hope to catch you soon from my next story.
Till then, stay safe and chill…!